簡體   English   中英

C# 使用 SharpZipLib 將文本文件 (.GetInputStream()) 從存檔解壓縮為字符串

[英]C# Unzip a textfile (.GetInputStream()) from an archive to string using SharpZipLib

如何將文本文件從 zip 存檔解壓縮為字符串?

文本文件是否也需要像二進制圖像一樣的MemoryStreamStreamReader

具體來說:

        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();
        }
    }

與任何流一樣,您可以使用環繞流的 StreamReader 並調用ReadToEnd

using(var x = new StreamReader(stream)){
  var theString =  x.ReadToEnd();
  //use theString
}

如果要指定編碼, 構造函數可以為該編碼采用第二個參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM