如何阅读zip文件内容?

时间:2011-09-21 09:34:27

标签: c#

如何在不解压缩的情况下从zip读取文件内容? 在zip中搜索文件名后,我想在window temp文件夹中提取文件并复制该文件,然后删除提取文件。 请回答我的问题。

1 个答案:

答案 0 :(得分:3)

您可以使用sharpziplib读取文件而无需将其写入磁盘。 可以这样做:

    public string Uncompress(string zipFile, string entryName)
    {
        string s = string.Empty;
        byte[] bBuffer = new byte[4096];
        ZipInputStream aZipInputStream = null;

        aZipInputStream = new ZipInputStream(File.OpenRead(zipFile));
        ZipEntry anEntry;
        while ((anEntry = aZipInputStream.GetNextEntry()) != null)
        {
            if (anEntry.Name == entryName)
            {
                MemoryStream aMemStream = new MemoryStream();
                int bSize;
                do
                {
                    bSize = aZipInputStream.Read(bBuffer, 0, bBuffer.Length);
                    aMemStream.Write(bBuffer, 0, bSize);
                }
                while (bSize > 0);
                aMemStream.Close();
                byte[] b = aMemStream.ToArray();
                s = Encoding.UTF8.GetString(b);
                aZipInputStream.CloseEntry();
                break;
            }
            else
                aZipInputStream.CloseEntry();
        }
        if (aZipInputStream != null)
            aZipInputStream.Close();
        return s;
    }