首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Delphi Rio 10.3.2:从ZIP文件中删除文件

Delphi Rio 10.3.2:从ZIP文件中删除文件
EN

Stack Overflow用户
提问于 2019-08-07 12:02:34
回答 1查看 311关注 0票数 1

如何从ZIP文件中删除文件?到目前为止,我已经尝试过各种库/组件,但都没有成功:

Delphi的TZip:不支持删除文件

KAzip: 10.3不起作用

Jcl (绝地):关闭档案后损坏

Abbrevia: 10.3 (损坏的文件)不起作用

有什么东西我可以使用完整的源代码和没有外部DLL?(使用TZip完全解压和重新包装除外)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-07 17:20:14

最后创建了我自己的proc (用于我自己的自定义库)。

tf =带有zip的TFileStream

cSize =压缩文件大小

它只对一个文件的删除有好处。

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57394070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档