简体   繁体   English

如何分阶段创建zip文件?

[英]How to create zip file in stages?

I'm writing data into ZIP file in a following way: 我通过以下方式将数据写入ZIP文件:

    private static void ZipStringToFile(string value)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true))
            {
                byte[] queryData = _encUtf8.GetBytes(value);
                compressedzipStream.Write(queryData, 0, queryData.Length);
                compressedzipStream.Write(_dataEndOfLine, 0, _dataEndOfLine.Length);
            }
            using (FileStream outfile = new FileStream("TestZip.zip", FileMode.Append))
            {
                byte[] buffer2 = new byte[ms.Length];
                ms.Seek(0, SeekOrigin.Begin);
                ms.Read(buffer2, 0, buffer2.Length);
                outfile.Write(buffer2, 0, buffer2.Length);
            }
        }
    }

That works quite well. 效果很好。 But if I use the same method once again: 但是,如果我再次使用相同的方法:

        ZipStringToFile("String #1");
        ZipStringToFile("\r\n");
        ZipStringToFile("String #2");

I have bigger zip file (so I believe file contains both strings), but after unpacking using "TotalCommander" embedded archiver I only see "String #2". 我有较大的zip文件(因此我认为文件包含两个字符串),但是使用“ TotalCommander”嵌入式存档程序解压缩后,我只会看到“ String#2”。

I believe that structure of zip-file itself doesn't allow "appending" so I have something principally wrong in my approach. 我认为zip文件本身的结构不允许“附加”,因此我的做法主要存在错误。

Should I just implement serializing everything into file first and zip IN THE END? 我是否应该先将所有内容序列化为文件并最终压缩为zip? Or there is proper way of appending data into zip archive? 还是有将数据附加到zip存档中的正确方法?

Thanks. 谢谢。

You are using the term "zip" incorrectly. 您使用的术语“ zip”不正确。 gzip is not zip. gzip不是zip。 gzip is a compressed data format for a single stream. gzip是单个流的压缩数据格式。 zip is an archive format for multiple files. zip是用于多个文件的存档格式。 You are generating gzip streams. 您正在生成gzip流。

It is possible concatenate gzip streams to make a larger, valid gzip stream. 可以合并gzip流以生成更大的有效gzip流。 A compliant gzip decoder should deliver all of the data from the concatenated streams. 兼容的gzip解码器应从级联流中传递所有数据。 So either you are not concatenating them properly, or you are not delivering the result of the concatenation properly to "TotalCommander", or "TotalCommander" is not a compliant gzip decoder. 因此,要么没有正确地串联它们,要么没有将正确的串联结果传递给“ TotalCommander”,或者“ TotalCommander”不是兼容的gzip解码器。

Should I just implement serializing everything into file first and zip IN THE END? 我是否应该先将所有内容序列化为文件并最终压缩为zip?

In general, yes, you should. 通常,是的,您应该这样做。 The reason is that you will get lousy compression if you only feed gzip little strings. 原因是,如果只喂gzip小字符串,您将得到糟糕的压缩。 In order to get good compression, you need to give gzip a lot more data to work with. 为了获得良好的压缩效果,您需要为gzip提供更多数据以供使用。

If you were using the ASP.NET ZipArchive library, my suggestion would be to try: 如果您使用的是ASP.NET ZipArchive库,我的建议是尝试:

  1. check to see if the zip exists 检查邮政编码是否存在
  2. If it exists, open it. 如果存在,请打开它。
  3. Use the GetEntry function to open the existing archive entry 使用GetEntry函数打开现有的存档条目
  4. Open() the entry to a stream, then write your new data to the stream Open()流的条目,然后将新数据写入流
  5. Save the archive back to disk. 将存档保存回磁盘。

Otherwise you're probably overwriting the existing archive each time. 否则,您可能每次都覆盖现有存档。

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

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