數據壓縮和解壓的示例代碼:
{壓縮流}
function CompressStream(ASrcStream: TStream; ALevel: TSfCompressionLevel): TStream;
var
SrcData,Buffer:Pointer;
BufSize:Integer;
begin
Buffer:=nil;
Result:=nil;
BufSize:=0;
GetMem(SrcData,ASrcStream.Size);
ASrcStream.Position:=0;
ASrcStream.Read(SrcData^,ASrcStream.Size);
try
try
SfCompressBuf(SrcData,ASrcStream.Size,Buffer,BufSize,ALevel);
except
on E:Exception do
SfRaiseException(E,'Exception raised in CompressStream call');
end;
finally
FreeMem(SrcData);
SrcData:=nil;
end;
//由於try...except塊中重引發了異常,所以在發生了異常的情況下,以下的代碼不會執行
Result:=TMemoryStream.Create;
Result.Write(Buffer^,BufSize);
FreeMem(Buffer);
end;
{解壓流}
function CompressStream(ASrcStream: TStream; ALevel: TSfCompressionLevel): TStream;
var
SrcData,Buffer:Pointer;
BufSize:Integer;
begin
Buffer:=nil;
Result:=nil;
BufSize:=0;
GetMem(SrcData,ASrcStream.Size);
ASrcStream.Position:=0;
ASrcStream.Read(SrcData^,ASrcStream.Size);
try
try
SfCompressBuf(SrcData,ASrcStream.Size,Buffer,BufSize,ALevel);
except
on E:Exception do
SfRaiseException(E,'Exception raised in CompressStream call');
end;
finally
FreeMem(SrcData);
SrcData:=nil;
end;
//由於try...except塊中重引發了異常,所以在發生了異常的情況下,以下的代碼不會執行
Result:=TMemoryStream.Create;
Result.Write(Buffer^,BufSize);
FreeMem(Buffer);
end;
{壓縮位元組數組}
function CompressBytes(ASrcBytes: TBytes; ALevel: TSfCompressionLevel): TBytes;
var
Buffer:Pointer;
BufSize:Integer;
begin
Buffer:=nil;
BufSize:=0;
try
SfCompressBuf(@ASrcBytes[0],Length(ASrcBytes),Buffer,BufSize,ALevel);
SetLength(Result,BufSize);
Move(Buffer^,Result[0],BufSize);
except
on E:Exception do
SfRaiseException(E,'Exception raised in CompressBytes call');
end;
//由於try...except塊中重引發了異常,所以在發生了異常的情況下,以下的代碼不會執行
FreeMem(Buffer);
end;
{解壓位元組數組}
function DecompressBytes(ASrcBytes: TBytes): TBytes;
var
Buffer:Pointer;
BufSize:Integer;
begin
Buffer:=nil;
BufSize:=0;
try
SfDecompressBuf(@ASrcBytes[0],Length(ASrcBytes),0,Buffer,BufSize);
SetLength(Result,BufSize);
Move(Buffer^,Result[0],BufSize);
except
on E:Exception do
SfRaiseException(E,'Exception raised in DecompressBytes call');
end;
//由於try...except塊中重引發了異常,所以在發生了異常的情況下,以下的代碼不會執行
FreeMem(Buffer);
end;
㈡ delphi解壓文件問題
呵呵,,我給你一個解決的辦法吧,看你的代碼就知道你是下載VCLZIP組件。。
建議你用VCLunzip組件:解壓代碼如下:
UnZip1.ZipName:='c:\Upx.zip';//zip所在目錄
UnZip1.ReadZip;
UnZip1.FilesList.Add('*.*');
UnZip1.FilesList.Add(UnZip1.Filename[UnZip1.Count-1] );
UnZip1.DoAll:=false;
UnZip1.DestDir:='c:\';//指定目錄
UnZip1.RecreateDirs := True;
UnZip1.RetainAttributes := True;
UnZip1.UnZip;
要設置解壓時替換原有的文件不彈出提示框,其實很簡單在OverwriteMode屬性設成Always就可以了!!
㈢ 如何使用delphi調用7zip壓縮解壓縮
e 解壓壓縮文件到當前目錄
忽略路徑解壓壓縮的文件到當前或指定的文件夾。此命令行對應的是 壓縮文件管理 命令 解壓到指定文件夾 並在 解壓路徑和選項對話框 「不解壓路徑」的選項是啟用時。
例子:
在當前文件夾,從全部的 RAR 壓縮文件解壓所有的 *.doc 文件到當前文件夾
WinRAR e *.rar *.doc
㈣ 怎麼用Delphi7實現文件夾的壓縮
var dbpathname:string;
AccessEngine :DBEngine;
begin
AccessEngine:=CoDBEngine.Create;
with dm_zt.Query_ZhTao do
begin
dbpathname:=fieldbyname('db_pathname').asstring;
end;
if FileExists(dbpathname) then
begin
try
try
AccessEngine:=CoDbEngine.Create;
if FileExists(extractfilepath(dbpathname)+'tempback.dat') then
deletefile(extractfilepath(dbpathname)+'tempback.dat')
else
begin
AccessEngine.CompactDatabase(dbpathname,extractfilepath(dbpathname)+'tempback.dat','',0,';pwd=B102901010816');
DeleteFile(dbpathname);
renameFile(extractfilepath(dbpathname)+'tempback.dat',dbpathname);
end;
except
ShowMessage('壓縮失敗');
// raise;
end;
finally
ShowMessage('壓縮成功');
end;
㈤ 在delphi中如何實現多文件或文件夾壓縮在一個壓縮包中,要詳解。
你說的這個壓縮包,是指像rar那樣的文件壓縮嗎?
如果是,請用winexec調用 rar的 dos命令。
如:
rar.exe a C:\test d:\test.rar
你要把 rar.exe 放在項目文件夾
㈥ delphi XE 10 解壓壓縮文件
沒有的,可以在前端要求加個判斷格式,只有ZIP的才行。或都把.rar的自己轉換成zip
㈦ Delphi 解壓文件
很簡單吧,先不帶密碼嘗試解壓一次或者獲取信息一次,如果失敗了就說明有密碼咯。
代碼:
procere TForm1.btn1Click(Sender: TObject);
begin
UnZip1.OnBadPassword:=UnZip1BadPassword;
UnZip1.ArchiveStream:=TFileStream.Create('F:\古書.zip',fmOpenRead);
if UnZip1.CheckArchive then
begin
showmessage('No Pass');
UnZip1.ArchiveStream.Free;
exit;
//無密碼,不解壓
end;
//解壓過程,略。
UnZip1.ArchiveStream.Free;
end;
procere TForm1.UnZip1BadPassword(Sender: TObject; FileIndex: Integer;
var NewPassword: String);
begin
NewPassword:='123456'; //123456是真實的密碼,這一步可以省
end;
㈧ delphi 怎麼來壓縮文件
在 delphi 里有多種方式來壓縮文件:
1、直接調用系統的壓縮軟體,比如 winrar,用 ShellExecute / Winexec 執行命令行:
"C:/ProgramFiles/WinRAR/WinRAR.exe"a "d:c.rar""D:Music"
2、使用 delphi 提供的單元文件 zlib.pas 中相關的壓縮函數:
示例代碼:
//壓縮函數
procereZip(varfs:TMemoryStream);
var
cs:TCompressionStream;
ms:TMemoryStream;
num:Integer;
begin
ifnot(Assigned(fs)and(fs.Size>0))thenExit;
num:=fs.Size;
ms:=TMemoryStream.Create;
cs:=TCompressionStream.Create(clMax,ms);
try
fs.SaveToStream(cs);
cs.Free;
//ms.Position:=0;
fs.Clear;
fs.WriteBuffer(num,sizeof(num));
fs.CopyFrom(ms,0);
finally
ms.Free;
end;
end;
3、使用專門的壓縮控制項,如VCLZip 等。
㈨ Delphi中如何執行ZIP文件謝謝!
前提條件:你必須裝有
WinZip
or
WinRAR
等解壓縮軟體,
並且文件已經關聯)即可使用:
樓上兄弟說的
ShellExecute
(記得
use
ShellApi
先)~
㈩ 在DELPHI中如何實現文件的「壓縮和解壓」
Delphi自帶有壓縮包,但是,沒有安裝,我寫了個調用程序,如果你需要,我的Email
:
[email protected]