【问题标题】:GZipStream zip file invalid or corruptedGZipStream zip 文件无效或损坏
【发布时间】:2009-08-25 12:34:39
【问题描述】:

我在打开 zip 文件时遇到问题。我正在使用此代码压缩文件:

public static string Zip_File(string soruce , string target)
{
    try
    {
        byte[] bufferWrite;               
        using (FileStream fsSource = new FileStream(soruce, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            bufferWrite = new byte[fsSource.Length];
            fsSource.Read(bufferWrite, 0, bufferWrite.Length);
            using (FileStream fsDest = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (GZipStream gzCompressed = new GZipStream(fsDest, CompressionMode.Compress, true))
                {
                    gzCompressed.Write(bufferWrite, 0, bufferWrite.Length);
                    bufferWrite = null;
                    fsSource.Close();
                    gzCompressed.Close();
                    fsDest.Close();
                }
            }
        }
        return "success";
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

当我调用此函数时,我收到“成功”消息,但我无法打开 zip 文件。这是我的函数调用代码:

ZipFiles.Zip_File(@"C:\Documents and Settings\ccspl\Desktop\IntegrityDVR.mdb", @"C:\Documents and Settings\ccspl\Desktop\a.zip")

这是我收到的错误消息:

压缩(文件夹)无效或损坏

【问题讨论】:

    标签: c# zip unzip gzipstream


    【解决方案1】:

    GZipStream 不会创建 .zip 文件。它创建.gz 文件。如果你需要创建.zip 文件,你应该使用SharpZipLib 之类的东西。

    【讨论】:

    • no Mehrdad ...我的问题尚未解决...它给出了相同的错误消息
    • RV:当然它不会创建 .ZIP 文件。你需要一个像 7-Zip 这样的程序来打开 .gz 文件。
    • Cheeso:我建议Flush 作为开始故障排除的一种方式。那是在我理解 OP 根本不需要 Gzip 之前。他想要 Zip。
    【解决方案2】:

    但是,等一下,GZipStream 不会创建 zip 文件,它会创建 gzip 文件,据我所知,Zipping files using GZipStream 应该会有所帮助

    【讨论】:

    【解决方案3】:

    为什么不使用SharpZipLib?它使这变得容易得多。

    【讨论】:

      【解决方案4】:

      DotNetZip 的示例代码,一个开源 zip 库。

      public static string ZipFile(String source, String target)
      {
          try 
          {
              using (ZipFile zip = new ZipFile()
              {
                  zip.AddFile(source);
                  zip.Save(target);
              }
              return "success";
          }
          catch {}
          return "failure";
      } 
      

      【讨论】:

      • +1 提供了一个很好且非常有用的库参考,感谢您的更正
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      相关资源
      最近更新 更多