使用dotnetzip库压缩流时损坏的文件

时间:2011-10-05 18:35:27

标签: c# .net compression dotnetzip

创建文件并且大小似乎没问题,但是当我双击它时说它的格式错误或文件已损坏。

这是我正在使用的代码

public MemoryStream CompressFiles(Dictionary<string, MemoryStream> filesToBeCompressed)
{
    var output = new MemoryStream();
    using (var zip = new ZipFile())
    {
        foreach (var entry in filesToBeCompressed)
        {
            entry.Value.Seek(0, SeekOrigin.Begin); // <-- must do this after writing the stream (I've read this in a blog
            zip.AddEntry(entry.Key.Substring(entry.Key.LastIndexOf('/') + 1, entry.Key.Length - entry.Key.LastIndexOf('/') - 1), entry.Value);
            zip.Save(output);
        }
    }
    return output;
}

然后在调用方法

SaveStreamToFile(documentCompressedName,getDocument());

getDocument()在内部调用Compress

最后那个方法

private static void SaveStreamToFile(string fileFullPath, Stream stream)
{
    if (stream.Length == 0) return;

    // Create a FileStream object to write a stream to a file
    using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
    {
        // Fill the bytes[] array with the stream data
        var bytesInStream = new byte[stream.Length];
        stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

        // Use FileStream object to write to the specified file
        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
    }
}

有什么想法吗? 提前致谢!吉列尔莫。

2 个答案:

答案 0 :(得分:3)

我认为问题出在你的函数SaveStreamToFile中。在将存档写入磁盘之前,必须将流的位置设置为开头:

private static void SaveStreamToFile(string fileFullPath, Stream stream)
{
  if (stream.Length == 0) return;

  // Set the position within the stream to the beginning of the stream
  stream.Seek(0, SeekOrigin.Begin);      

  // Create a FileStream object to write a stream to a file
  using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
  {
    // Fill the bytes[] array with the stream data
    var bytesInStream = new byte[stream.Length];
    stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

    // Use FileStream object to write to the specified file
    fileStream.Write(bytesInStream, 0, bytesInStream.Length);
  }
}

希望,这有帮助。

答案 1 :(得分:1)

从你的代码片段中,我的猜测是当你将它传递给Position时,MemoryStream的SaveStreamToFile位于流的末尾,并且你从未将位置设置回到开头在流中,你的stream.Read实际上根本没有读取任何字节。如果您使用十六进制编辑器打开输出zip文件,您可能会看到它充满了零。

这里有很多选项,但我的建议是尝试:

private static void SaveStreamToFile(string fileFullPath, Stream stream)
{
    if (stream.Length == 0) return;

    // Create a FileStream object to write a stream to a file
    using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
    {
        // Use FileStream object to write to the specified file
        fileStream.Write(stream.GetBuffer(), 0, stream.Length);
    }
}

此方法避免获取MemoryStream的内部缓冲区的副本。虽然我不知道你的zip文件有多大,所以它在内存使用方面可能不是问题,但是将zip文件存储在内存中两次 - 一次放在MemoryStream中,再一次放在原始bytesInStream中。 1}}数组似乎没必要。