-3

我使用此代码将目录中的所有文件合并为一个,而不是真正的压缩,

procedure CompressDirectory(InDir : string; OutStream : TStream);
var
AE : TArchiveEntry;
procedure RecurseDirectory(ADir : string);
var
sr : TSearchRec;
TmpStream : TStream;
begin
if FindFirst(ADir + '*', faAnyFile, sr) = 0 then
begin
repeat
if (sr.Attr and (faDirectory or faVolumeID)) = 0 then
begin
// We have a file (as opposed to a directory or anything
// else). Write the file entry header.
AE.EntryType := aeFile;
AE.FileNameLen := Length(sr.Name);
AE.FileLength := sr.Size;
OutStream.Write(AE, SizeOf(AE));
OutStream.Write(sr.Name[1], Length(sr.Name));
// Write the file itself
TmpStream := TFileStream.Create(ADir + sr.Name, fmOpenRead or fmShareDenyWrite);
OutStream.CopyFrom(TmpStream, TmpStream.Size);
TmpStream.Free;
end;
if (sr.Attr and faDirectory) > 0 then
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
// Write the directory entry
AE.EntryType := aeDirectory;
AE.DirNameLen := Length(sr.Name);
OutStream.Write(AE, SizeOf(AE));
OutStream.Write(sr.Name[1], Length(sr.Name));
// Recurse into this directory
RecurseDirectory(IncludeTrailingPathDelimiter(ADir + sr.Name));
end;
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
// Show that we are done with this directory
AE.EntryType := aeEOD;
OutStream.Write(AE, SizeOf(AE));
end;
begin
RecurseDirectory(IncludeTrailingPathDelimiter(InDir));
end;

如果我希望 compressDirectory 函数不包含某些文件夹和文件怎么办?CompressDirectory 函数代码是什么样的?请指导我,谢谢。

> 为空间编辑、删除图像。

4

1 回答 1

2

该代码已经采用了一种技术来跳过某些不需要的文件名:

if (sr.Name <> '.') and (sr.Name <> '..') then

只需使用相同的技术来排除您希望的任何其他文件。您可以将排除列表硬编码到代码中,就像使用. ..名称,或者您可以将名称列表作为另一个参数传递给函数。在将文件添加到存档之前,请检查该文件名是否在要排除的文件列表中。

例如,如果排除的名称列表在TStrings后代中,您可以使用如下内容:

if ExcludedNames.IndexOf(sr.Name) >= 0 then
  Continue; // Skip the file because we've been told to exclude it.

您可以增强它以检查文件的完整路径,而不仅仅是本地名称。您还可以增强它以支持排除列表中的通配符。

于 2013-07-30T20:25:30.173 回答