您的位置:首页 > 其它

how to read file by ZipFile

2018-02-06 18:10 309 查看
download dll:

icsharpcode.sharpziplib.dll

put it into the directory unity Plugin directory.



write a path tool to get the file directory. this tool we will fill in the future.

now the code is short and not full.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

public static class PathUtil
{
public static string ToForwardSlash(this string path)
{
return path.Replace(@"\", @"/");
}

public static string ToSingleForwardSlash(this string path)
{
return path.Replace(@"//", @"/");
}

public static string ConverRelativePathToEditorOutputPath(string relativePath)
{
string platformPath = string.Empty;
#if UNITY_ANDROID
platformPath = "Android/";
#elif UNITY_IOS
platformPath = "iOS";
#else
platformPath = "Windows";
#endif
string outputPath = Path.Combine(Application.dataPath, "../TempData/" + platformPath).ToForwardSlash().ToSingleForwardSlash();
return Path.Combine(outputPath, relativePath.ToSingleForwardSlash().ToForwardSlash()).ToForwardSlash().ToSingleForwardSlash();
}
}


last we will write a demo script the read from zip file. zip file is like this:



C# testing code is :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using ICSharpCode.SharpZipLib.Zip;

public class ZipFileRead : MonoBehaviour
{
private ZipFile m_zipFile;
private void OnGUI()
{
if(Input.GetKeyDown(KeyCode.A))
{
string fileName = "a.zip";
string filePath = PathUtil.ConverRelativePathToEditorOutputPath(fileName);
if(File.Exists(filePath))
{
byte[] zipBytes = File.ReadAllBytes(filePath);
m_zipFile = new ZipFile(new MemoryStream(zipBytes));
}

ZipEntry zipEntry = m_zipFile.GetEntry("a.txt");

Debug.Log(zipEntry.Name);
}
}
}


the entry in the zip can be xml. txt. and others.

will fulfill.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zip file