Delphi功能,压缩期间不允许主目录下的文件和文件夹

编程入门 行业动态 更新时间:2024-10-11 23:25:05
本文介绍了Delphi功能,压缩期间不允许主目录下的文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

程序CompressDirectory(InDir:字符串; OutStream:TStream); var AE:TArchiveEntry; procedure RecurseDirectory(ADir:string); var sr:TSearchRec; TmpStream:TStream; begin 如果FindFirst(ADir +'*',faAnyFile,sr)= 0,那么 begin repeat if(sr.Attr和(faDirectory或faVolumeID) )= 0然后 begin //我们有一个文件(而不是一个目录或任何 // else)。写入文件条目标题。 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)); //写入文件 TmpStream:= TFileStream.Create(ADir + sr.Name,fmOpenRead或fmShareDenyWrite); OutStream.CopyFrom(TmpStream,TmpStream.Size); TmpStream.Free; 结束 if(sr.Attr and faDirectory)> 0然后 begin if(sr.Name<>'。')和(sr.Name<>'..')然后 begin //写目录条目 AE.EntryType:= aeDirectory; AE.DirNameLen:= Length(sr.Name); OutStream.Write(AE,SizeOf(AE)); OutStream.Write(sr.Name [1],Length(sr.Name)); //重新进入此目录 RecurseDirectory(IncludeTrailingPathDelimiter(ADir + sr.Name)); 结束结束 until FindNext(sr)<> 0; FindClose(sr); 结束 //显示我们完成了这个目录 AE.EntryType:= aeEOD; OutStream.Write(AE,SizeOf(AE)); 结束 begin RecurseDirectory(IncludeTrailingPathDelimiter(InDir)); 结束

如果我想要compressDirectory功能不包含一些文件夹和文件怎么办? CompressDirectory函数代码是什么样的?请指导我,谢谢。

> 编辑,删除图片空格。

解决方案

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

if(sr.Name<>'。')和(sr.Name<>'..')然后 / pre>

只需使用相同的技术排除您想要的任何其他文件。您可以将排除列表硬编码到代码中,如已经使用 和 .. 名称所做的那样,或者您可以将名称列表传递到函数中另一个参数。在将文件添加到存档之前,请检查该文件名是否在要排除的文件列表中。

例如,如果排除的名称列表在 c后代,您可能会使用这样的东西:

如果排除的名称。 IndexOf(sr.Name)> = 0然后继续; //跳过文件,因为我们被告知排除它。

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

I used this code to combined all files into one from a directory, not really compression,

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;

What if I want the compressDirectory function not to include some folders and files? What would be the CompressDirectory function code look like? please guide me, thanks.

> Edited, removed image for space.

解决方案

The code already employs a technique for skipping certain unwanted file names:

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

Simply use that same technique to exclude whatever other files you wish. You can hard-code the exclusion list into the code, as is already done with the . and .. names, or you can pass a list of names into the function as another parameter. Before you add a file to the archive, check whether that file name is in the list of files to exclude.

For example, if the list of excluded names were in a TStrings descendant, you might use something like this:

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

You could enhance this to check the full path of the file instead of just the local name. You could also enhance it to support wildcards in the list of exclusions.

更多推荐

Delphi功能,压缩期间不允许主目录下的文件和文件夹

本文发布于:2023-11-28 15:00:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不允许   文件夹   主目录   功能   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!