使用delphi 2009在vista上创建目录/文件的zip文件的任何函数
-
06-07-2019 - |
题
我正在寻找一种使用delphi进行压缩和压缩的简单方法。我已经看过torry delphi的组件: http://www.torry.net /pages.php?s=99 。它们似乎都可以完成我想要的但是使用它们的一些缺点是它们都没有在delphi 2009中运行并且非常复杂,这使我很难将它们移植到delphi 2009.此外,文档上它们很稀缺,至少对我而言。我需要基本的压缩功能,而不需要使用一堆DLL的开销。我的任务引导我到FSCTL_SET_COMPRESSION,我认为这可以解决这个问题,但不幸的是,这也没有用。 CREATEFILE看起来很有前途,直到我尝试它产生与FSCTL_SET相同的结果......我知道在Windows上有一些有限的本机压缩功能。例如,如果右键单击文件或文件夹并选择 - > sendTo - >压缩文件夹,智能创建压缩存档。我想如果我能够从delphi访问该功能,它将是一个解决方案。在一个侧面问题上,linux是否有自己的本机压缩函数可以使用类似的呢?
其他提示
我使用Zipforge。为什么将这些移植到D2009有问题?是因为64bit ??
以下是一些示例代码
procedure ZipIt;
var
Archiver: TZipForge;
FileName: String;
begin
try
Archiver:= TZipForge.create(self);
with Archiver do begin
FileName := 'c:\temp\myzip.zip';
// Create a new archive file
OpenArchive(fmCreate);
// Set path to folder with some text files to BaseDir
BaseDir := 'c:\temp\';
// Add all files and directories from 'C:\SOURCE_FOLDER' to the archive
AddFiles('myfiletozip.txt');
// Close the archive
CloseArchive;
end;
finally
Archiver.Free;
end;
end;
如果你能“做”来自Delphi的COM,那么你可以利用Windows shell的内置zip功能。它为您提供了良好的基本功能。
在VBScript中它看起来像这样:
Sub CreateZip(pathToZipFile, dirToZip)
WScript.Echo "Creating zip (" & pathToZipFile & ") from folder (" & dirToZip & ")"
Dim fso
Set fso= Wscript.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(pathToZipFile) Then
WScript.Echo "That zip file already exists - deleting it."
fso.DeleteFile pathToZipFile
End If
If Not fso.FolderExists(dirToZip) Then
WScript.Echo "The directory to zip does not exist."
Exit Sub
End If
NewZip pathToZipFile
dim sa
set sa = CreateObject("Shell.Application")
Dim zip
Set zip = sa.NameSpace(pathToZipFile)
WScript.Echo "opening dir (" & dirToZip & ")"
Dim d
Set d = sa.NameSpace(dirToZip)
For Each s In d.items
WScript.Echo s
Next
' http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx
' ===============================================================
' 4 = do not display a progress box
' 16 = Respond with "Yes to All" for any dialog box that is displayed.
' 128 = Perform the operation on files only if a wildcard file name (*.*) is specified.
' 256 = Display a progress dialog box but do not show the file names.
' 2048 = Version 4.71. Do not copy the security attributes of the file.
' 4096 = Only operate in the local directory. Don't operate recursively into subdirectories.
WScript.Echo "copying files..."
zip.CopyHere d.items, 4
' wait until finished
sLoop = 0
Do Until d.Items.Count <= zip.Items.Count
Wscript.Sleep(1000)
Loop
End Sub
COM也促使您使用 DotNetZip ,这是一个免费下载的密码加密拉链, zip64,自解压档案,unicode,拉链和其他东西。
就我个人而言,我使用的VCL Zip与D2009和D2010完美搭配。这篇文章的确花费120美元,但非常简单,灵活,最重要的是快速。
如果您有兴趣,请查看 VCLZIP 并下载路径
代码明智:
VCLZip1.ZipName := ‘myfiles.zip’;
VCLZip1.FilesList.add(‘c:\mydirectory\*.*’);
VCLZip1.Zip;
是基本zip所需的全部内容,您当然可以设置压缩级别,目录结构,zip流,解压缩流等等。
希望这有一些帮助。
RE
请参阅此 OpenSource SynZip部件。解压缩比Delphi附带的默认单元更快,它会生成一个更小的exe(crc表在启动时创建)。
不需要外部dll。适用于Delphi 6至XE。 Unicode版本的Delphi没问题。全部在一个单元中。
我刚刚对Zip内容中的Unicode文件名进行了一些更改,不仅包括Win-Ansi字符集,还包括任何Unicode字符。欢迎提供反馈。