文件压缩(50分)

  • 主题发起人 主题发起人 jxgawjf
  • 开始时间 开始时间
J

jxgawjf

Unregistered / Unconfirmed
GUEST, unregistred user!
请教一个问题:我想把一个目录里的所有文件压缩到一个文件中<br>去
代码:
[?]应该怎么做。请指教
 
有个控件,什么vclzip之类的就行了.
 
以下程序调试通过!我将所有文件都压缩成1.36M!(你可以修改大小)<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, Math, StdCtrls, ComCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; ProgressBar1: TProgressBar;<br>&nbsp; &nbsp; Button4: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button4Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure InitPack(ss:string); &nbsp;//初始化打包文件<br>&nbsp; &nbsp; procedure PackFile(ss:string); &nbsp;//将文件打包。即文件名为ss的文件复制到打包文件中<br>&nbsp; &nbsp; procedure DePackFile(ss:string);<br>&nbsp; &nbsp; procedure BackUpto(ss:string);<br>&nbsp; &nbsp; function chkdisk(drive:byte;bbsize:longint):boolean;<br>&nbsp; &nbsp; function chkfile(ss:string):boolean;<br><br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>uses Unit2;<br><br>{$R *.dfm}<br>function Tform1.chkfile(ss:string):boolean;<br>var bn:integer;<br>begin<br>&nbsp; result:=true;<br>&nbsp; while fileexists(ss)=false do<br>&nbsp; begin<br>&nbsp; &nbsp; bn:=messagedlg('备份文件不存在!继续吗?',mtwarning,[mbyes,mbno],0);<br>&nbsp; &nbsp; if bn&lt;&gt;mryes then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>procedure Tform1.InitPack(ss:string); //初始化打包文件<br>var vf:File;<br>begin<br>assignfile(vf,ss);<br>rewrite(vf,1);<br>closefile(vf);<br>end;<br><br>procedure Tform1.packfile(ss:string);//将文件打包。即文件名为ss的文件复制到打包文件中<br>var rf,pf:File;<br>&nbsp; &nbsp; fsize:longint;<br>&nbsp; &nbsp; numread,numwrite:integer;<br>&nbsp; &nbsp; ss1,ss2,ss3:string[100];<br>&nbsp; &nbsp; buf:array[1..2048]of char;<br>begin<br>assignfile(pf,'d:/mypack.pak');//分配打包文件<br>assignfile(rf,ss); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //分配被打包文件<br>reset(pf,1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //打开打包文件<br>reset(rf,1); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //打开被打包文件<br>fsize:=filesize(pf); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //获取打包文件尺寸<br>seek(pf,fsize); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//文件指针移至文件尾<br>fsize:=filesize(rf); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //获取被打包文件尺寸<br>seek(rf,0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//文件指针移至文件尾<br>ss1:='CHANGE'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //标志<br>ss2:=ss;<br>str(fsize,ss3);<br>blockwrite(pf,ss1,10);<br>blockwrite(pf,ss2,30);<br>blockwrite(pf,ss3,20);<br>form1.Canvas.TextOut(50,200,'文件 '+ss+' 正在打包!'); &nbsp; &nbsp; //******<br>repeat &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //复制文件<br>&nbsp; blockread(rf,buf,sizeof(buf),numread);<br>&nbsp; blockwrite(pf,buf,numread,numwrite);<br>until (numread=0)or(numwrite&lt;&gt;numread);<br>closefile(pf);<br>closefile(rf);<br>end;<br><br>procedure Tform1.BackUpto(ss:string);//打包文件ss复制到备份目录,mysize<br>var sf,df:File;<br>&nbsp; &nbsp; nnn,nn,basesize,srcfilesize:longint;<br>&nbsp; &nbsp; dnum,vi,bn:integer;<br>&nbsp; &nbsp; ss1:string[200];<br>&nbsp; &nbsp; ssdnum,sste,ssattr:string[20];<br>&nbsp; &nbsp; vbyte:char;<br>&nbsp; &nbsp; buf:array[1..2048]of char;<br>begin<br>assignfile(sf,ss);<br>reset(sf,1);<br>srcfilesize:=filesize(sf);<br>basesize:=1430000; &nbsp;//定义软盘的大小<br>dnum:=ceil(srcfilesize/basesize); &nbsp;//计算所需的软盘数量<br>str(dnum,ssdnum);<br>randomize; &nbsp; &nbsp; //启动随机数产生器<br>vi:=random(1000); &nbsp; &nbsp; //产生一个随机数<br>str(vi,ssattr);<br>for vi:=1 to dnum do &nbsp; //复制到软盘或其他目录<br>&nbsp;begin<br>&nbsp; &nbsp;str(vi,sste);<br>&nbsp; &nbsp;sste:=trim(sste);<br>&nbsp; &nbsp;if (edit1.text='A:') or (edit1.text='A:/') or (edit1.text='a:') or (edit1.text='a:/') then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ss1:='共需'+ssdnum+'张盘,请插入第'+sste+'张盘。';<br>&nbsp; &nbsp; &nbsp; &nbsp; bn:=messagedlg(ss1,mtinformation,[mbyes,mbno],0);<br>&nbsp; &nbsp; &nbsp; &nbsp; if bn&lt;&gt;mryes then break;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;ss1:=uppercase(trim(edit1.text));<br>&nbsp; &nbsp;vbyte:=ss1[1];<br>&nbsp; &nbsp;bn:=ord(vbyte)-ord('A')+1;<br>&nbsp; &nbsp;if chkdisk(bn,basesize)=false then break;<br>&nbsp; &nbsp;ss1:=trim(edit1.text)+'/xx'+sste+'disk.bak';<br>&nbsp; &nbsp;assignfile(df,ss1);<br>&nbsp; &nbsp;rewrite(df,1);<br>&nbsp; &nbsp;ss1:='XXSOFT001';<br>&nbsp; &nbsp;blockwrite(df,ss1,16);<br>&nbsp; &nbsp;blockwrite(df,ssdnum,16);<br>&nbsp; &nbsp;blockwrite(df,ssattr,16);<br>&nbsp; &nbsp;srcfilesize:=basesize;<br>&nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp;if srcfilesize&gt;2040 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;nn:=2040;<br>&nbsp; &nbsp; &nbsp; &nbsp;srcfilesize:=srcfilesize-2040;<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;nn:=srcfilesize;<br>&nbsp; &nbsp; &nbsp; &nbsp;srcfilesize:=0;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;blockread(sf,buf,nn,nnn);<br>&nbsp; &nbsp; &nbsp;blockwrite(df,buf,nnn);<br>&nbsp; &nbsp;until(srcfilesize&lt;=0);<br>&nbsp; &nbsp;closefile(df);<br>&nbsp; &nbsp;progressbar1.StepBy(floor(70/dnum));<br>&nbsp;end;<br>&nbsp;closefile(sf);<br>end;<br><br>procedure Tform1.DePackFile(ss:string); &nbsp;//解包<br>var sf,df:file;<br>&nbsp; &nbsp; ss1,ss2,ss3:string[40];<br>&nbsp; &nbsp; fsize:longint;<br>&nbsp; &nbsp; numread,numwrite,nn:integer;<br>&nbsp; &nbsp; buf:array[1..2048]of char;<br>begin<br>&nbsp; assignfile(sf,ss);<br>&nbsp; reset(sf,1);<br>&nbsp; while not eof(sf) do<br>&nbsp; begin<br>&nbsp; &nbsp; blockread(sf,ss1,10,nn);<br>&nbsp; &nbsp; blockread(sf,ss2,30,nn);<br>&nbsp; &nbsp; blockread(sf,ss3,20,nn);<br>&nbsp; &nbsp; if nn&lt;20 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showmessage('No size');<br>&nbsp; &nbsp; &nbsp; closefile(sf);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; fsize:=strtoint(ss3);<br>&nbsp; &nbsp; if ss1&lt;&gt;'CHANGE' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; showmessage('No CHANGE');<br>&nbsp; &nbsp; &nbsp; closefile(sf);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; assignfile(df,ss2);<br>&nbsp; &nbsp; rewrite(df,1);<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; if fsize&gt;2000 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; nn:=2000;<br>&nbsp; &nbsp; &nbsp; &nbsp; fsize:=fsize-2000;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; nn:=fsize;<br>&nbsp; &nbsp; &nbsp; &nbsp; fsize:=0;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; blockread(sf,buf,nn,numread);<br>&nbsp; &nbsp; &nbsp; blockwrite(df,buf,numread,numwrite);<br>&nbsp; &nbsp; until (fsize=0)or(numwrite&lt;&gt;numread);<br>&nbsp; &nbsp; closefile(df);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; closefile(sf);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>progressbar1.Min:=0;<br>progressbar1.Max:=100;<br>initpack('d:/mypack.pak');<br>packfile('d:/1.XLs');<br>packfile('d:/2.XLs');<br>packfile('d:/3.XLs');<br>packfile('d:/4.XLs');<br>packfile('d:/5.XLs');<br>packfile('d:/6.XLs');<br>packfile('d:/7.XLs');<br>packfile('d:/8.XLs');<br>packfile('d:/9.XLs');<br>packfile('d:/10.XLs');<br>packfile('d:/11.XLs');<br>packfile('d:/12.XLs');<br>backupto('d:/mypack.pak');<br>form1.Canvas.TextOut(50,200,' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;');<br>button2.SetFocus;<br>end;<br><br>function Tform1.chkdisk(drive:byte;bbsize:longint):boolean;//软盘检查,成功返回真。<br>var bn,chk:integer;<br>&nbsp; &nbsp; mysize,sup:longint;<br>begin<br>&nbsp; result:=true;<br>&nbsp; &nbsp; chk:=0;<br>&nbsp; &nbsp; while chk=0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; mysize:=diskfree(drive);<br>&nbsp; &nbsp; &nbsp; if mysize=-1 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; bn:=messagedlg('未插入盘,请插入!',mtwarning,[mbyes,mbno],0);<br>&nbsp; &nbsp; &nbsp; &nbsp; if bn=mryes then continue<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; sup:=diskfree(drive) div 1000000;<br>&nbsp; &nbsp; &nbsp; &nbsp; if sup&lt;1.36 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bn:=messagedlg('磁盘空间不够,请插入另一张盘!',mtwarning,[mbyes,mbno],0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if bn=mryes then continue<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>close;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br>form2.Show;<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>var sf,df:file;<br>&nbsp; &nbsp; ss:string[200];<br>&nbsp; &nbsp; ss1,ssattr:string[30];<br>&nbsp; &nbsp; numread,numwrite,bn,nn:integer;<br>&nbsp; &nbsp; buf:array[1..2048]of char;<br>begin<br>ss:=trim(edit1.Text)+'/xx1disk.bak';<br>if fileexists(ss)=false then<br>&nbsp;begin<br>&nbsp; &nbsp;messagedlg('备份文件不存在!',mtwarning,[mbok],0);<br>&nbsp; &nbsp;exit;<br>&nbsp;end;<br>assignfile(df,'d:/mypack.pak');<br>rewrite(df,1);<br>assignfile(sf,ss);<br>reset(sf,1);<br>blockread(sf,ss1,16);<br>if ss1&lt;&gt;'XXSOFT001' then<br>&nbsp;begin<br>&nbsp; &nbsp;messagedlg('不是该系统的备份文件!',mtwarning,[mbok],0);<br>&nbsp; &nbsp;closefile(sf);<br>&nbsp; &nbsp;exit;<br>&nbsp;end;<br>blockread(sf,ss1,16);<br>nn:=strtoint(ss1);<br>blockread(sf,ssattr,16);<br>repeat<br>&nbsp; blockread(sf,buf,sizeof(buf),numread);<br>&nbsp; blockwrite(df,buf,numread,numwrite);<br>until (numread=0)or(numwrite&lt;&gt;numread);<br>closefile(sf);<br>&nbsp;if nn&gt;1 then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;for bn:=2 to nn do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;if (edit1.text='A:') or (edit1.text='A:/') or (edit1.text='a:') or (edit1.text='a:/') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messagedlg('请插入第'+inttostr(bn)+'张盘!',mtinformation,[mbok],0);<br>&nbsp; &nbsp; &nbsp; &nbsp;ss:=trim(edit1.Text)+'/xx'+trim(inttostr(bn))+'disk.bak';<br>&nbsp; &nbsp; &nbsp; &nbsp;if chkfile(ss)=false then break;<br>&nbsp; &nbsp; &nbsp; &nbsp;assignfile(sf,ss);<br>&nbsp; &nbsp; &nbsp; &nbsp;reset(sf,1);<br>&nbsp; &nbsp; &nbsp; &nbsp;blockread(sf,ss1,16);<br>&nbsp; &nbsp; &nbsp; &nbsp;if ss1&lt;&gt;'XXSOFT001' then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;messagedlg('不是该系统的备份文件!',mtwarning,[mbok],0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;closefile(sf);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;closefile(df);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;blockread(sf,ss1,16);<br>&nbsp; &nbsp; &nbsp; &nbsp;blockread(sf,ss1,16);<br>&nbsp; &nbsp; &nbsp; &nbsp;if ss1&lt;&gt;ssattr then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messagedlg('文件xx'+inttostr(bn)+'disk.bak与其它文件不是同一次的备份文件!',mtwarning,[mbok],0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closefile(sf);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closefile(df);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blockread(sf,buf,sizeof(buf),numread);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blockwrite(df,buf,numread,numwrite);<br>&nbsp; &nbsp; &nbsp; &nbsp;until (numread=0)or(numwrite&lt;&gt;numread);<br>&nbsp; &nbsp; &nbsp; &nbsp;closefile(sf);<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; end;<br>closefile(df);<br>depackfile('d:/mypack.pak');<br>button2.SetFocus;<br>end;<br><br>end.
 
后退
顶部