【问题标题】:C# ZipFile throwing exceptions after successfull operation [duplicate]C# ZipFile在成功操作后抛出异常[重复]
【发布时间】:2018-03-21 09:02:00
【问题描述】:

我正在尝试使用 .NET 4.5.2 中的 ZipFile 将图像文件夹(2000 左右)压缩为 .zip 文件

以下代码成功创建 .zip 文件:

static void Main(string[] args)
{
    string startPath= @"D:\Photos";
    string zipPath = @"D:\Photos\all.zip";

    //Same issue with CompressionLevel.Optimal
    ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, false);

    Console.Write("Done! ");
    Console.ReadKey();
}

创建 zip 文件并填充所有图像,但随后该方法开始抛出异常,一个 System.IO.IOException in System.IO.Compression.FileSystem.dll 告诉我该文件夹已被另一个进程使用,然后 System.IO.IOException in mscorlib.dll 告诉我 all.zip 已经存在于目标目录,这个被不断抛出,似乎没有尽头。

任何帮助将不胜感激!

【问题讨论】:

  • 可能问题在于压缩输出与要压缩的文件位于同一文件夹中。该函数尝试压缩文件D:\Photos\all.zip,因为文件掩码未指定不应压缩此文件。
  • 谢谢!我不认为这会是个问题,我更改了目标文件夹,现在它可以正常工作了,尽管我更愿意为图像和 zip 使用相同的文件夹。
  • @HiSpy 你以后可以随时移动它
  • @phuzi 这可能是我要做的,我想我对文件操作的新意正在显现!

标签: c# .net zipfile


【解决方案1】:

这对我有用:

 string startPath = @"E:\testPage";
 string zipPath = @"E:\testPage.zip";

 //Same issue with CompressionLevel.Optimal
 ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, false);

 Console.Write("Done! ");
 Console.ReadKey();

您正在读取文件夹并在同一路径中创建 zip,因此您收到错误该文件夹已被另一个进程使用

更改您的 zip 路径。然后它应该可以工作。

【讨论】:

    猜你喜欢
    • 2011-01-22
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2011-10-01
    • 2012-07-21
    相关资源
    最近更新 更多