⑴ 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 解压文件
很简单吧,先不带密码尝试解压一次或者获取信息一次,如果失败了就说明有密码咯。
代码:
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自带有压缩包,但是,没有安装,我写了个调用程序,如果你需要,我的Email
:
[email protected]
⑷ delphi中用zlib怎样压缩和解压
数据压缩和解压的示例代码:
{压缩流}
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 怎么来压缩文件
在 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是否有压缩和解压字符串函数
delphi 内置有 zlib 单元(高版本为 system.zlb),uses 该单元后,可以使用其中的两个功能函数来完成,以下是示例:
procereTForm1.Button1Click(Sender:TObject);
var
tb:陆颂TBytes;
s:string;
begin
孝缓tb:=ZCompressStr(Edit1.Text);
s:=ZDecompressStr(tb);
ShowMessage('解压后的字符串巧悉模:'+s);
end;
运行截图:
⑺ delphi 怎么来压缩文件
使用时,需要Zlib.pas和 Zlibconst.pas两个单元文件,这两个文件保存在 Delphi 5.0安装光盘上
InfoExtrasZlib目录下,此外,在 InfoExtrasZlibObj目录中还保存了 Zlib.pas单元引用的
Obj文件,把这个目录拷贝到delphi的lib下,即可。可以适当的改动比如增加目录压缩和分文件压缩,其实就是在文件流前面增加一部分描述结构就是,不多说。使用
时,还要use zlib单元。 两个函数如下:
procere CompressIt(var CompressedStream: TMemoryStream; const CompressionLevel: TCompressionLevel);
// 参数是传递的流和压缩方式
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: int64; //注意,此处修改了,原来是int
begin
//获得流的原始尺寸
Count := CompressedStream.Size;
DestStream := TMemoryStream.Create;
SourceStream := TCompressionStream.Create(CompressionLevel, DestStream);
try
//SourceStream中保存着原始的流
CompressedStream.SaveToStream(SourceStream);
//将原始流进行压缩, DestStream中保存着压缩后的流
SourceStream.Free;
CompressedStream.Clear;
//写入原始图像的尺寸
CompressedStream.WriteBuffer(Count, SizeOf(Count));
//写入经过压缩的流
CompressedStream.CopyFrom(DestStream, 0);
finally
DestStream.Free;
end;
end;
procere UnCompressit(const CompressedStream: TMemoryStream; var UnCompressedStream: TMemoryStream);
//参数 压缩过的流,解压后的流
var
SourceStream: TDecompressionStream;
DestStream: TMemoryStream;
Buffer: PChar;
Count: int64;
begin
//从被压缩的图像流中读出原始的尺寸
CompressedStream.ReadBuffer(Count, SizeOf(Count));
//根据尺寸大小为将要读入的原始流分配内存块
GetMem(Buffer, Count);
DestStream := TMemoryStream.Create;
SourceStream := TDecompressionStream.Create(CompressedStream);
try
//将被压缩的流解压缩,然后存入 Buffer内存块中
SourceStream.ReadBuffer(Buffer^, Count);
//将原始流保存至 DestStream流中
DestStream.WriteBuffer(Buffer^, Count);
DestStream.Position := 0; //复位流指针
DestStream.Position := length(VER_INFO);
//从DestStream流中载入图像流
UnCompressedStream.LoadFromStream(DestStream);
finally
FreeMem(Buffer);
DestStream.Free;
end;
end;
使用的例子如下:
procere TForm1.Button5Click(Sender: TObject);
//把指定文件压缩然后保存为另外一个压缩包,
//呵呵,我使用的时候是把后缀改成cab,可以唬一些人吧?
var
SM: TMemoryStream;
begin
if OpenDialog1.Execute then
begin
if SaveDialog1.Execute then
begin
SM := TMemoryStream.Create;
try
Sm.LoadFromFile(OpenDialog1.FileName);
SM.Position := 0;
Compressit(sm, clDefault);
sm.SaveToFile(SaveDialog1.FileName);
finally
SM.Free;
end;
end;
end;
end;
procere TForm1.BitBtn2Click(Sender: TObject);
//把指定的压缩包解成原来的文件。
var
SM, DM: TMemoryStream;
begin
if OpenDialog1.Execute then
begin
if SaveDialog1.Execute then
begin
SM := TMemoryStream.Create;
DM := TMemoryStream.Create;
try
Sm.LoadFromFile(OpenDialog1.FileName);
SM.Position := 0;
UnCompressit(sm, dm);
dm.Position := 0;
dm.SaveToFile(SaveDialog1.FileName);
finally
SM.Free;
DM.Free;
end;
end;
end;
end;