简体   繁体   English

如果我将文件打包打包,则GZipStream会损坏文件(刷新不起作用)

[英]GZipStream corrupts file if i pack it in parts (Flush doesn't work)

I've tried to use this code to compress a file in parts 我试图使用此代码来部分压缩文件

using (var fsIn = new FileStream("test.avi", FileMode.Open))
{
    using (var fsOut = new FileStream("test.avi.gz", FileMode.Create))
    {
        var buf = new byte[1024 * 1024];

        using (var gzip = new GZipStream(fsOut, CompressionMode.Compress, true))
        {
            while (true)
            {
                var readCount = fsIn.Read(buf, 0, buf.Length);
                if (readCount <= 0)
                {
                    break;
                }

                gzip.Write(buf, 0, buf.Length);
                gzip.Flush();
            }
        }
    }
}

but i've got corrupted file after decompression. 但解压后我的文件已损坏。 This code works 此代码有效

using (var fsIn = new FileStream("test.avi", FileMode.Open))
{
    using (var fsOut = new FileStream("test.avi.gz", FileMode.Create))
    {
        var buf = new byte[1024*1024];

        while (true)
        {
            var readCount = fsIn.Read(buf, 0, buf.Length);
            if (readCount <= 0)
            {
                break;
            }

            // This string was transferred into "while" cycle
            using (var gzip = new GZipStream(fsOut, CompressionMode.Compress, true))
            {
                gzip.Write(buf, 0, buf.Length);
            }
        }
    }
}

Why gzip.Flush() doesn't work? 为什么gzip.Flush()不起作用? Why only gzip.Close() works? 为什么只有gzip.Close()有效?

new GZipStream(fsOut, CompressionMode.Compress, true) leaves the stream open after disposing, you should change the last parameter to false . new GZipStream(fsOut, CompressionMode.Compress, true)在处置后将流保持打开状态,您应该将最后一个参数更改为false

GZipStream Constructor (Stream, CompressionMode, Boolean) GZipStream构造函数(Stream,CompressionMode,布尔值)

leaveOpen
Type: System.Boolean 类型: System.Boolean
true to leave the stream open after disposing the GZipStream object; true离开流开放处置后GZipStream对象; otherwise, false . 否则为false

Also Flush() has no effects in GZipStream 另外Flush()在GZipStream中没有任何作用

The current implementation of this method does not flush the internal buffer. 此方法的当前实现不刷新内部缓冲区。 The internal buffer is flushed when the object is disposed. 放置对象时,将清空内部缓冲区。

Yuo need to know that gzip.Flush(); 您需要知道gzip.Flush(); is not working 不管用

From MSDN - GZipStream.Flush : MSDN-GZipStream.Flush

The current implementation of this method does not flush the internal buffer. 此方法的当前实现不刷新内部缓冲区。 The internal buffer is flushed when the object is disposed. 放置对象时,将清空内部缓冲区。

File is compressed properly. 文件已正确压缩。 in fact it turns out that the file contains a set of compressed files. 实际上,事实证明该文件包含一组压缩文件。 Archives number equal to the number parts. 档案编号等于部分编号。 At each new iteration, a new file and appended to the file. 在每次新迭代时,都会有一个新文件并附加到该文件中。 decision 决定

You need to specify the length of the packaging after the packaging and before burning to file. 您需要在包装后和刻录到文件之前指定包装的长度。

Just like that: 就像这样:

Compress: 压缩:

BitConverter.GetBytes(compressedPart.Length).CopyTo(blockToWrite, 4);

In decompression read this length and choose part of the file is equal to this length for decompression. 在解压缩中,读取此长度,然后选择文件的一部分等于该长度以进行解压缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM