1

我一直在使用 JclCompression 库,试图将文件添加到现有存档中。不幸的是,当archive.compress在下面的函数中调用时,它会删除原始存档并将其替换为仅包含我尝试添加的单个压缩文件的存档(具有相同的文件名)。所有原始文件已被删除。

function TMyCompression.AddZipItems(const archiveFileName: string; const itemsToZip: TList<TMyCompressionItem>): boolean;
var
  archive: TJclUpdateArchive;
  archiveClass: TJclUpdateArchiveClass;
  packedItemName : string;
  i: Integer;
begin
  archiveClass := GetArchiveFormats.FindUpdateFormat(archiveFileName);
  archive := archiveClass.Create(archiveFileName);
  if NOT Assigned(archive) then
    Result := FALSE
  else
    try
      for i := 0 to itemsToZip.Count -1 do
        begin
          packedItemName := RootPath(itemsToZip[i].Name);
          if itemsToZip[i].IsDirectory then
            archive.AddDirectory(packedItemName, itemsToZip[i].Name, true, true)
          else
            archive.AddFile(packedItemName, itemsToZip[i].Name);
        end;
      archive.Compress;
    finally
      archive.Free;
    end;
 end;

有没有办法用 JclCompression 工具来完成这个?

编辑:

删除了几个不影响代码的内部函数调用。

我尝试过但拒绝的其他 Delphi 压缩工具:

  • Delphi XE的内部System.Zip;任何类型的进度或反馈都没有挂钩。

  • TurboPower 缩写 v5;几十年来,我已经成功地使用了这个工具和旧的 zip32 压缩。但是,我现在需要 zip64 支持,并且最新版本在我尝试的前两个超过 4GB 的文件上失败了(我记得这是一个无效的块大小)。这两个文件都可以使用 7-zip 和 Windows 文件资源管理器打开。

  • Shell 到 Windows 文件资源管理器使用的内部 zip 工具;我让一个 6GB 的压缩文件在一夜之间运行,当我早上返回时,它仍然报告还有 20 小时的运行时间。

7-Zip 速度极快,可以提供良好的反馈,并且已经过彻底的练习和测试。

4

1 回答 1

0

所有原始文件已被删除

使用ListFiles方法

procedure ArchivingLog;
var
  file_name,
  archive_name   : string;
  archive        : TJclCompressArchive;
  archive_format : TJclCompressArchiveClass;
begin
  file_name := format('%s\111.log', [GetCurrentDir]);

  if (FileExists(file_name)) then
  begin
    archive_name   := GetCurrentDir + '\111.log.7z';
    archive_format := GetArchiveFormats.FindUpdateFormat(archive_name);

    if archive_format <> nil then
    begin
      archive := archive_format.Create(archive_name);

      if (FileExists(archive_name)) then
        (archive as TJcl7zUpdateArchive).ListFiles; //without this call deletes all previous files in the archive


      (archive as TJcl7zUpdateArchive).AddFile(ExtractFileName(file_name), file_name);

      try
        (archive as TJcl7zUpdateArchive).Compress;
      finally
        FreeAndNil(archive);
      end;
    end;
  end;
end;
于 2016-12-25T12:41:02.417 回答