|
|
|
用DELPHI实现文件加密压缩 |
热 ★★★★ |
|
用DELPHI实现文件加密压缩 |
|
作者:闵涛 文章来源:闵涛的学习笔记 点击数:2198 更新时间:2009/4/23 18:30:59 |
|
用来自解压的可执行程序文件 2. 将1中建立的文件,生成资源文件 3. 将资源文件放到本文中这个压缩工具的程序中一起编译。 4. 通过将资源文件与压缩文件的合并,生成自解压文件。 自解压文件结构图:
5.自解压实现:通过将自身文件中的加密压缩数据的分解,然后对分解的加密压缩数据再一次解压并分解出真正的数据文件。
2.6 系统程序设计
这是关于这个软件实现的核心部分全部代码,在这里详细讲述这个软件所有的技术细节。 // wnhoo_zzz.pas
unit wnhoo_zzz; interface
uses Windows,Forms,SysUtils,Classes,zlib,Registry,INIFILES, Dialogs, shlobj; type pass=string[20]; type Tmyzip = class
private { private declarations here} protected { protected declarations here } public procedure regzzz; procedure ys_file(infileName, outfileName: string;password:pass;isjm:boolean;ysbz:integer); function jy_file(infileName: string;password:pass=''''''''):boolean; procedure zjywj(var filename:string); constructor Create; destructor Destroy; override; { public declarations here } published { published declarations here } end;
implementation
constructor Tmyzip.Create; begin inherited Create; // 初始化继承下来的部分 end;
//##################################################### //原文件加密 procedure jm_File(vfile:string;var Target:TMemoryStream;password:pass;isjm:boolean); { vfile:加密文件 target:加密后输出目标流 》》》 password:密码 isjm:是否加密 ------------------------------------------------------------- 加密后文件SIZE=原文件SIZE+[INI加密压缩信息文件]的SIZE+存储[INI加密压缩信息文件]的大小数据类型的SIZE --------------------------------------------------------------- } var
tmpstream,inistream:TFileStream; FileSize:integer; inifile:TINIFILE; filename:string; begin //打开需要 [加密压缩文件] tmpstream:=TFileStream.Create(vFile,fmOpenread or fmShareExclusive); try //向 [临时加密压缩文件流] 尾部写入 [原文件流] Target.Seek(0,soFromEnd); Target.CopyFrom(tmpstream,0); //取得文件路径 ,生成 [INI加密压缩信息文件] filename:=ExtractFilePath(paramstr(0))+''''tmp.in_''''; inifile:=TInifile.Create(filename); inifile.WriteString(''''file1'''',''''filename'''',ExtractFileName(vFile)); inifile.WriteString(''''file1'''',''''password'''',password); inifile.WriteInteger(''''file1'''',''''filesize'''',Target.Size); inifile.WriteDateTime(''''file1'''',''''fileDate'''',now()); inifile.WriteBool(''''file1'''',''''isjm'''',isjm); inifile.Free ; //读入 [INI加密压缩信息文件流] inistream:=TFileStream.Create(filename,fmOpenread or fmShareExclusive); try //继续在 [临时加密压缩文件流] 尾部加入 [INI加密压缩信息文件] inistream.Position :=0; Target.Seek(0,sofromend); Target.CopyFrom(inistream,0); //计算当前 [INI加密压缩信息文件] 的大小 FileSize:=inistream.Size ; //继续在 [临时加密文件尾部] 加入 [INI加密压缩信息文件] 的SIZE信息 Target.WriteBuffer(FileSize,sizeof(FileSize)); finally inistream.Free ; deletefile(filename); end; finally tmpstream.Free; end;
end;
//**************************************************************
//流压缩 procedure ys_stream(instream, outStream: TStream;ysbz:integer); { instream: 待压缩的已加密文件流 outStream 压缩后输出文件流 ysbz:压缩标准 } var ys: TCompressionStream; begin //流指针指向头部 inStream.Position := 0; //压缩标准的选择 case ysbz of 1: ys := TCompressionStream.Create(clnone,OutStream);//不压缩 2: ys := TCompressionStream.Create(clFastest,OutStream);//快速压缩 3: ys := TCompressionStream.Create(cldefault,OutStream);//标准压缩 4: ys := TCompressionStream.Create(clmax,OutStream); //最大压缩 else
ys := TCompressionStream.Create(clFastest,OutStream); end;
try //压缩流 ys.CopyFrom(inStream, 0); finally ys.Free; end; end;
//*****************************************************************
//流解压 procedure jy_Stream(instream, outStream: TStream); { instream :原压缩流文件 outStream:解压后流文件 } var jyl: TDeCompressionStream; buf: array[1..512] of byte; sjread: integer; begin inStream.Position := 0; jyl := TDeCompressionStream.Create(inStream); try repeat //读入实际大小 sjRead := jyl.Read(buf, sizeof(buf)); if sjread > 0 then OutStream.Write(buf, sjRead); until (sjRead = 0); finally jyl.Free; end; end;
//**************************************************************
//实现关联注册 procedure Tmyzip.regzzz; var reg: TRegistry; begin reg := TRegistry.Create; reg.RootKey := HKEY_CLASSES_ROOT; reg.OpenKey(''''.zzz'''', true); reg.WriteString('''''''', ''''myzip''''); reg.CloseKey; reg.OpenKey(''''myzip\shell\open\command'''', true); //用于打开.zzz文件的可执行程序 reg.WriteString('''''''', ''''"'''' + application.ExeName + ''''" "%1"''''); reg.CloseKey; reg.OpenKey(''''myzip\DefaultIcon'''',true); //取当前可执行程序的图标为.zzz文件的图标 reg.WriteString('''''''',''''''''+application.ExeName+'''',0''''); reg.Free; //立即刷新 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
end;
//压缩文件 procedure Tmyzip.ys_file(infileName, outfileName: string;password:pass;isjm:boolean;ysbz:integer); { infileName://需要压缩加密的文件 outfileName://压缩加密后产生的文件 password://解压密码 ysbz://压缩标准 } var instream:TMemoryStream; //文件加密后的临时流 outStream: TFileStream; //压缩输出文件流
begin //创建 [文件加密后的临时流] instream:=TMemoryStream.Create; //文件加密 jm_file(infileName,instream,password,isjm); //创建压缩输出文件流 outStream := TFileStream.create(outFIleName, fmCreate); try //[文件加密后的临时流] 压缩 ys_stream(instream,OutStream,ysbz); finally OutStream.free; instream.Free ; end; end;
//解压文件 function Tmyzip.jy_file(infileName: string;password:pass=''''''''):boolean; var inStream,inistream,filestream_ok: TFileStream; { instream://解压文件名称 inistream://INI临时文件流 filestream_ok://解压OK的文件 } outStream:tmemorystream; //临时内存流 inifile:TINIFILE; //临时INI文件 FileSize:integer; //密码文件的SIZE resultvalue:boolean;//返回值
begin
try inStream := TFileStream.create(inFIleName, fmOpenRead);
try outStream := tmemorystream.create; try jy_stream(insTream,OutStream); //生成临时INI文件 inistream:=TFileStream.create(ExtractFilePath(paramstr(0))+''''tmp.in_'''', fmCreate); try //指向存储解码信息的INTEGER型变量位置 OutStream.Seek(-sizeof(FileSize),sofromend); //读入变量信息 OutStream.ReadBuffer(FileSize,sizeof(FileSize)); //指向解码信息位置 OutStream.Seek(-(FileSize+sizeof(FileSize)),sofromend); //将解码信息读入INI流中 inistream.CopyFrom(OutStream,FileSize); //释放INI文件流 inistream.Free ; //读入INI文件信息 inifile:=TINIFILE.Create(ExtractFilePath(paramstr(0))+''''tmp.in_''''); resultvalue:=inifile.ReadBool(''''file1'''',''''isjm'''',false); if resultvalue then begin if inifile.ReadString (''''file1'''',''''password'''','''''''')=trim(password) then resultvalue:=true else &n上一页 [1] [2] [3] 下一页 没有相关教程
|
|
教程录入:mintao 责任编辑:mintao |
|
|
上一篇教程: Delphi部分函数、命令、属性中文说明 下一篇教程: 用Delphi实现整个网站图片的极速下载 |
|
|
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网] |
网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
|
|
|
|
|
|
|
同类栏目 |
|
|
赞助链接 |
|
|
500 - 内部服务器错误。
|
|
|
|
|
|