【问题标题】:Zipping a folder压缩文件夹
【发布时间】:2012-06-19 12:44:47
【问题描述】:

我正在尝试使用

TZipFile.ZipDirectoryContents()

像这样:

TZipFile.ZipDirectoryContents('Test.PCF', WorkingDir);

如果我没看错,它应该将文件夹“workingdir”的内容保存到名为 Test.pcf 的文件中。

现在当我这样做时,我得到了错误::

Raised exception class EFOpenError with message Cannot open file
...test.pcf. The process cannot access the file because its being used by another process."

有两件事让我感到困惑:

  1. 它说它无法打开文件。还没有 test.pcf。我希望这会创造它。

  2. 它说无法访问文件。这是因为它还没有创建吗?我在使用这个功能吗?如果是这样,我将如何从文件夹位置创建 zip 文件?

【问题讨论】:

  • 同样,我对 TZipFile 不熟悉,但“Test.PCF”是否在您尝试压缩的目录中?

标签: delphi delphi-xe


【解决方案1】:

我测试了你的代码,结果和你报告的一样失败了。

然后我通过运行 WinZip 手动创建了一个空的 zip 文件。

然后运行你的代码,它运行良好。

看来 zip 文件必须已经存在才能使 ZipDirectoryContents 工作。

以编程方式创建 zip 文件:

  myZipFile := TZIpFile.Create;
  myZipFile.Open('c:\myfolder\newzipfile.zip', TZipMode.zmWrite);
  myZipFile.Close;
  myZipFile.Free;

然后这条线将起作用:

  TZipFile.ZipDirectoryContents('c:\myfolder\newzipfile.zip', WorkingDir);

【讨论】: