[英]C# Unzip a textfile (.GetInputStream()) from an archive to string using SharpZipLib
如何將文本文件從 zip 存檔解壓縮為字符串?
文本文件是否也需要像二進制圖像一樣的MemoryStream
或StreamReader
?
具體來說:
foreach (ZipEntry e in zipFile)
{
if (e.IsFile)
{
string ext = Path.GetExtension(e.Name).ToLower();
print(ext + " " + e.Name);
if (ext == ".jpg" || ext == ".png" || ext == ".tga")
{
Texture2D tex = new Texture2D(1, 1);
Stream zipStream = zipFile.GetInputStream(e);
tex.LoadImage(ReadFully(zipStream));
dicTexture.Add(e.Name,tex );
} else if (ext == ".txt")
{
Stream zipStream = zipFile.GetInputStream(e);
dicTxt.Add(e.Name, Encoding.Default.GetString(zipStream.)); // how do you populate this string here?
}
}
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.