using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using YKMoon.ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace YKMoon.IO
{
public partial class IOTools
{
///
/// Get ZipStream from "Assets/Resources".
///
/// base on "Assets/Resources" without extension.
/// zip password.
/// if true will throw exception else return null when catch any exception.
/// null if resource not exist.when autoThrowException is false and has exception will return null.
public static ZipInputStream GetZipStreamFromResources(string resourcePath, string password = "", bool autoThrowException = false)
{
//ZipConstants.DefaultCodePage = 0;
ZipInputStream outputStream = null;
Stream stream = null;
try {
TextAsset textAsset = Resources.Load(resourcePath) as TextAsset;
stream = new MemoryStream(textAsset.bytes);
outputStream = new ZipInputStream(stream);
outputStream.Password = password;
outputStream.GetNextEntry();
return outputStream;
} catch(System.Exception e) {
Debug.LogException(e);
if(stream != null) {
stream.Close();
stream.Dispose();
stream = null;
}
if(outputStream != null) {
outputStream.Close();
outputStream.Dispose();
outputStream = null;
}
if(autoThrowException) {
throw e;
} else {
return null;
}
}
}
///
/// Get ZipStream from .
///
/// full path with file extension.
/// zip password.
/// if true will throw exception else return null when catch any exception.
/// null if resource not exist.when autoThrowException is false and has exception will return null.
public static ZipInputStream GetZipFileStream(string fullPath, string password="", bool autoThrowException = false)
{
ZipInputStream outputStream = null;
FileStream fileStream = null;
try {
fileStream = File.OpenRead(fullPath);
outputStream = new ZipInputStream(fileStream);
outputStream.Password = password;
outputStream.GetNextEntry();
return outputStream;
} catch(System.Exception e) {
Debug.LogException(e);
if(fileStream != null) {
fileStream.Close();
fileStream.Dispose();
fileStream = null;
}
if(outputStream != null) {
outputStream.Close();
outputStream.Dispose();
outputStream = null;
}
if(autoThrowException) {
throw e;
} else {
return null;
}
}
}
}
}