GZipStream和解压缩

时间:2009-10-17 07:58:42

标签: c# gzipstream compression

我有应该进行压缩的代码:

FileStream fs = new FileStream("g:\\gj.txt", FileMode.Open);
FileStream fd = new FileStream("g:\\gj.zip", FileMode.Create);
GZipStream csStream = new GZipStream(fd, CompressionMode.Compress);

byte[] compressedBuffer = new byte[500];
int offset = 0;
int nRead;

nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
while (nRead > 0)
{
    csStream.Write(compressedBuffer, offset, nRead);
    offset = offset + nRead;
    nRead = fs.Read(compressedBuffer, offset, compressedBuffer.Length);
}

fd.Close();
fs.Close();

我认为确实如此,但我想解压缩上面压缩的内容。我这样想:

FileStream fd = new FileStream("g:\\gj.new", FileMode.Create);
FileStream fs = new FileStream("g:\\gj.zip", FileMode.Open);
GZipStream csStream = new GZipStream(fs, CompressionMode.Decompress);

byte[] decompressedBuffer = new byte[500];
int offset = 0;
int nRead;

nRead=csStream.Read(decompressedBuffer, offset, decompressedBuffer.Length);
while (nRead > 0)
{
    fd.Write(decompressedBuffer, offset, nRead);
    offset = offset + nRead;
    nRead = csStream.Read(decompressedBuffer, offset, decompressedBuffer.Length);
}

fd.Close();
fs.Close();

在这里它没有...我有nRead = 0 befeore进入循环...我做错了什么? 我使用的测试文件是最简单的TEXT文件(大小:104字节)......

3 个答案:

答案 0 :(得分:16)

我的第一个想法是你没有关闭csStream。如果您使用using,则会自动执行此操作。由于gzip缓冲数据,你可能会遗漏一些。

其次;不要增加offset;这是缓冲区(不是流)中的偏移量。离开0:

using (Stream fs = File.OpenRead("gj.txt"))
using (Stream fd = File.Create("gj.zip"))
using (Stream csStream = new GZipStream(fd, CompressionMode.Compress))
{
    byte[] buffer = new byte[1024];
    int nRead;
    while ((nRead = fs.Read(buffer, 0, buffer.Length))> 0)
    {
        csStream.Write(buffer, 0, nRead);
    }
}

using (Stream fd = File.Create("gj.new.txt"))
using (Stream fs = File.OpenRead("gj.zip"))
using (Stream csStream = new GZipStream(fs, CompressionMode.Decompress))
{
    byte[] buffer = new byte[1024];
    int nRead;
    while ((nRead = csStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fd.Write(buffer, 0, nRead);
    }
}

答案 1 :(得分:0)

我所拥有的两种方法就像詹姆斯罗兰提到的那样。

private static byte[] Compress(HttpPostedFileBase file)
{
    var to = new MemoryStream();
    var from = new GZipStream(to, CompressionMode.Compress);
    file.InputStream.CopyTo(from);
    from.Close();
    return to.ToArray();
}

private byte[] Decompress(byte [] img)
{
    var to = new MemoryStream();
    var from = new MemoryStream(img);
    var compress = new GZipStream(from, CompressionMode.Decompress);
    compress.CopyTo(to);
    from.Close();
    return to.ToArray();
}

但是,我正在使用

上传
Request.Files[0] 

然后压缩并保存在db中。然后我拉出img,解压缩并用

设置一个src
$"data:image/gif;base64,{ToBase64String(Decompress(img))}";

答案 2 :(得分:0)

下面的代码演示了如何使用GZipStream进行压缩和解压缩:

using System;
using System.IO;
using System.IO.Compression;

// ...
    static void Main(string[] args)
    {
        string folder = @"c:\temp";
        string dataPath = Path.Combine(folder, "data.dat");
        string compressedPath = Path.Combine(folder, "compressed.gz");
        string decompressedPath = Path.Combine(folder, "decompressed.dat");

        using (FileStream fileReader = File.OpenRead(dataPath))
        using (FileStream fileWriter = File.Create(compressedPath))
        using (GZipStream compressionStream = new GZipStream(fileWriter, CompressionMode.Compress))
        {
            // Compresses and writes byte array contents to file

            int readlength = 0;
            byte[] buffer = new byte[1024];

            do
            {
                readlength = fileReader.Read(buffer, 0, buffer.Length);
                compressionStream.Write(buffer, 0, readlength);

            } while (readlength > 0);
        }

        using (FileStream fileReader = File.OpenRead(compressedPath))
        using (FileStream fileWriter = File.OpenWrite(decompressedPath))
        using (GZipStream compressionStream = new GZipStream(fileReader, CompressionMode.Decompress))
        {
            // Decompresses and reads data from stream to file

            int readlength = 0;
            byte[] buffer = new byte[1024];

            do
            {
                readlength = compressionStream.Read(buffer, 0, buffer.Length);
                fileWriter.Write(buffer, 0, readlength);

            } while (readlength > 0);
        }

        FileInfo dataFile = new FileInfo(dataPath);
        FileInfo compressedFile = new FileInfo(compressedPath);
        FileInfo decompressedFile = new FileInfo(decompressedPath);
        Console.WriteLine($"Uncompressed file size: {dataFile.Length} bytes");
        Console.WriteLine($"Compressed file size: {compressedFile.Length} bytes");
        Console.WriteLine($"Decompressed file size: {decompressedFile.Length} bytes");

        Console.Write("Press any key to quit . . . ");
        Console.ReadKey(true);
    }