如何从ZIP文件中删除文件?到目前为止,我已经尝试过各种库/组件,但都没有成功:
Delphi的TZip:不支持删除文件
KAzip: 10.3不起作用
Jcl (绝地):关闭档案后损坏
Abbrevia: 10.3 (损坏的文件)不起作用
有什么东西我可以使用完整的源代码和没有外部DLL?(使用TZip完全解压和重新包装除外)
发布于 2019-08-07 17:20:14
最后创建了我自己的proc (用于我自己的自定义库)。
tf =带有zip的TFileStream
cSize =压缩文件大小
它只对一个文件的删除有好处。
procedure RemoveFile(name: string);
Var a,b: NativeInt;
tm: TMemoryStream;
RemovedSize,c: Cardinal;
const szChar = sizeof(AnsiChar);
begin
// Locate File
for a := Low(FileHeaders) to High(FileHeaders) do
if CompareText(FileHeaders[a].fileName, name) = 0 then break;
if a > High(FileHeaders) then Exit; // not found
tm := TMemoryStream.Create;
tf.Seek32(0, soBeginning);
// copy up to the file to be removed
tm.CopyFrom(tf, CentralDirs[a].headerOffset);
// skip the file
With FileHeaders[a] do
RemovedSize := 30 + filenameLength + extraFieldLength + cSize;
tf.Seek32(RemovedSize, soCurrent); // seek forward
// update offsets for the next files
for b := a+1 to High(FileHeaders) do
Dec(CentralDirs[b].headerOffset, RemovedSize);
// copy rest of the files
c := 0;
for b := a+1 to High(FileHeaders) do
With FileHeaders[b] do
Inc(c, 30 + filenameLength + extraFieldLength + cSize);
tm.CopyFrom(tf, c);
EOCD.centralDirOffset := tm.Position;
// write Dir headers
for c := Low(CentralDirs) to High(CentralDirs) do begin
// skip removed file
if c = a then Continue;
With CentralDirs[c] do begin
tm.Write(CentralDirs[c], 46);
tm.Write(Pointer( CentralDirs[c].fileName )^, szChar * CentralDirs[c].filenameLength);
tm.Write(Pointer( CentralDirs[c].extraField )^, szChar * CentralDirs[c].extraFieldLength);
tm.Write(Pointer( CentralDirs[c].fileComment )^, szChar * CentralDirs[c].fileCommentLength);
end;
end;
// Update EOCD data, copy
With CentralDirs[a] do
Dec(EOCD.centralDirSize, 46 + filenameLength + extraFieldLength + fileCommentLength);
Dec(EOCD.numRecordsOnDisk);
Dec(EOCD.numRecords);
tm.Write(EOCD, 22);
tm.Write(Pointer( EOCD.comment )^, szChar * EOCD.commentLength );
tm.SaveToFile('test.zip');
tm.Free;
end;https://stackoverflow.com/questions/57394070
复制相似问题