13

我试图理解为什么我的代码没有按预期执行。它创建一个 GZipStream,然后将对象作为压缩文件保存在我的硬盘上,但保存的文件始终为 0 字节。

现在我知道如何使用 GZipStream 保存文件,但是,我的问题不是如何去做。我的问题纯粹是为什么这段代码会保存 0 个字节(或者为什么 FileStream 有效而内存无效)。

private void BegingCompression()
{
    var bytes = File.ReadAllBytes(this.fileName);
    using (MemoryStream ms = new MemoryStream(bytes))
    {
        ms.ReadByte();
        using (FileStream fs =new FileStream(this.newFileName, FileMode.CreateNew))
        using (GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress, false))
        {
            zipStream.Write(bytes, 0, bytes.Length);
        }
    }
}

关于源代码,this.fileName = c:\Audio.wav并且newFileNamec:\Audio.wav.gz(但也尝试过c:\audio.gz

4

4 回答 4

17
  • 您不需要 MemoryStream,因为bytes已经有要压缩的数据。
  • ms.ReadByte()不应该使用。
  • 创建zipStream输出文件时应使用流。

试试这个:

var bytes = File.ReadAllBytes(this.fileName);
using (FileStream fs =new FileStream(this.newFileName, FileMode.CreateNew))
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress, false))
{
     zipStream.Write(bytes, 0, bytes.Length);
}

编辑

原始代码创建一个零长度文件,因为您不写入文件流。

于 2013-01-12T19:40:22.887 回答
11

当您使用GzipStreamDeflateStream来自System.IO.Compression命名空间时,Stream您在构造函数中提供的将被写入以进行压缩decompression中读取。

由于您尝试在此处压缩数据,因此使用MemoryStream是不正确的,因为您没有尝试对其进行压缩,而是将其用作数据源。因此,您MemoryStream应该是输入Stream,而应该FileStream是您的输出。

我强烈建议您使用MemoryStream作为原始数据源的数据源,byte[]因为Stream它具有更多的多功能性和应用程序(FileStream, NetworkStream,CryptoStream等)

以下是一些使用async/await模式的示例:

public static async Task CompressToFileAsync(byte[] buffer, 
                                             string outputFile)
{
  using (var inputStream = new MemoryStream(buffer))
    await CompressToFileAsync(inputStream, outputFile);
}

public static async Task CompressToFileAsync(Stream inputStream, 
                                             string outputFile)
{
  using (var outputStream = File.Create(outputFile))
  using (var gzip = new GZipStream(outputStream, CompressionMode.Compress))
  {
    await inputStream.CopyToAsync(gzip);
    gzip.Close();
  }
}

public static async Task<MemoryStream> DecompressFromFileAsync(string inputFile)
{
  var outputStream = new MemoryStream();

  using (var inputStream = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  using (var gzip = new GZipStream(inputStream, CompressionMode.Decompress))
  {
    await gzip.CopyToAsync(outputStream);

    gzip.Close();
    inputStream.Close();

    // After writing to the MemoryStream, the position will be the size
    // of the decompressed file, we should reset it back to zero before returning.
    outputStream.Position = 0;
    return outputStream;
  }
}

注意:始终在关闭输入输出GzipStream.Close()之前调用。它在关闭/处置时会进行一些最终的缓冲区刷新,如果输入输出首先关闭,它会在尝试这样做时抛出异常。(这也适用于) StreamDeflateStream

于 2013-01-12T20:40:16.503 回答
6

我使用这个类进行压缩/解压缩:

internal class GZipProcessor : IZipProcessor
{

    public byte[] Compress(byte[] data)
    {
        using (var compressedStream = new MemoryStream())
        {
            using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
            {
                zipStream.Write(data, 0, data.Length);
                zipStream.Close();
                return compressedStream.ToArray();
            }
        }
    }

    public byte[] Decompress(byte[] data)
    {
        using (var compressedStream = new MemoryStream(data))
        {
            using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
            {
                using (var resultStream = new MemoryStream())
                {
                    zipStream.CopyTo(resultStream);
                    return resultStream.ToArray();
                }
            }
        }
    }

}

以及我如何使用它:

public void Compress(string inputPath, string outputPath)
{
    byte[] originalBytes = File.ReadAllBytes(inputPath);
    byte[] zippedBytes = base.ZipProcessor.Compress(originalBytes);
    File.WriteAllBytes(outputPath, zippedBytes);
}

public void Decompress(string inputPath, string outputPath)
{
    byte[] zippedBytes = File.ReadAllBytes(inputPath);
    byte[] originalBytes = base.ZipProcessor.Decompress(zippedBytes);
    File.WriteAllBytes(outputPath, originalBytes);
}

我也有一个github 存储库,其中包含更复杂的代码

于 2018-05-04T15:23:07.617 回答
3
why FileStream works and memory doesn't

因为 MemoryStream 的后备存储是您的 RAM 内存,而不是硬盘驱动器。因此,当您将MemoryStream对象传递给GZipStream构造函数时,您的 gzip 将只是在 RAM 中。

使用此代码:

using (FileStream fs =new FileStream(this.newFileName, FileMode.CreateNew))

您正在创建新的 FileStream,但您没有使用它。要使用此 FIleStream 存储 gzip,您需要将其传递给GZipStream构造函数,以使其后备存储成为硬盘驱动器。

于 2013-01-12T19:38:45.103 回答