从Azure Blob存储解压缩压缩文件

时间:2019-02-01 12:07:23

标签: c# zip unzip sharpziplib

我想解压缩文件并从Azure blob存储中存在的压缩文件中提取内容。

我正在使用SharpZipLib软件包来实现此目的。 我遇到了多个错误,最新的错误是“不支持InflaterInputStream位置”。 我想念什么吗?

public static string DeCompressZipAndGetContent(Stream zipStream)
    {
        var content = string.Empty;
        zipStream.Position = 0;
        ZipInputStream zipInputStream = new ZipInputStream(zipStream);
        ZipEntry zipEntry = zipInputStream.GetNextEntry();
        while (zipEntry != null) 
        {
            String entryFileName = zipEntry.Name;
            byte[] buffer = new byte[4096];
            using (Stream outStream = new MemoryStream()) 
            {
                CopyFromOneStreamToAnother(zipInputStream, outStream, buffer);
                outStream.Position = 0;
                using (StreamReader reader = new StreamReader(outStream, Encoding.UTF8))
                {
                    content = reader.ReadToEnd();
                }
            }
        }

        return content;
    }

private static void CopyFromOneStreamToAnother(Stream source, Stream destination, byte[] buffer)
    {
        bool copying = true;
        while (copying)
        {
            int bytesRead = source.Read(buffer, 0, buffer.Length);
            if (bytesRead > 0)
                destination.Write(buffer, 0, bytesRead);
            else
            {
                destination.Flush();
                copying = false;
            }
        }
    }

0 个答案:

没有答案