我一直在与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压缩工具:
7-Zip是非常快,允许良好的反馈,并已充分行使和测试。
发布于 2016-12-25 12:41:02
所有原始文件都已被删除。
使用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;https://stackoverflow.com/questions/31688077
复制相似问题