我以前开发过的部分源码<br>可以压缩文件、文件夹<br><br><br> TCompFileInfo = record //压缩文件记录<br> Name : string; //文件名<br> Attr : string; //属性<br> Content : string; //内容<br> end;<br><br><br>procedure AddDirToPackage(SouDir,DestFile:string);<br>var FileList:TStrings;<br> i:integer;<br> FileInfo:TCompFileInfo;<br>begin<br> if DirectoryExists(SouDir)=False then exit;<br> FileList:=TStringList.Create;<br> try<br> FindFile(IncludeTrailingPathDelimiter(SouDir)+'*.*',True,True,faAnyFile,FileList);<br> for i:=0 to FileList.Count-1 do<br> AddFileToPackage(FileList.Strings,DestFile,ExtractFilePath(SouDir));<br> finally<br> FileList.Free;<br> end;<br>end;<br><br><br>procedure CompressFile(FileName:string;var Str:string);<br>var<br> SourceStream: TCompressionStream;<br> FileStream,DestStream: TMemoryStream;<br> Count:LongWord;<br>Begin<br> if FileExists(FileName)=False then exit;<br> DestStream := TMemoryStream.Create;<br> FileStream := TMemoryStream.Create;<br> FileStream.LoadFromFile(FileName);<br> Count := FileStream.Size;<br> SourceStream:=TCompressionStream.Create(clFastest,DestStream);<br> Try<br> FileStream.SaveToStream(SourceStream);<br> SourceStream.Free;<br> FileStream.Clear;<br> FileStream.WriteBuffer(Count,SizeOf(Count));<br> FileStream.CopyFrom(DestStream, 0);<br> Count := FileStream.Size;<br> FileStream.Position:=0;<br> SetLength(Str,Count);<br> FileStream.ReadBuffer(Str[1],Count);<br> finally<br> DestStream.Free;<br> FileStream.Free;<br> end;<br>end;<br><br>function File_AddString(iFileHandle:integer;Str:String):Boolean;<br>var<br> FilePos,RecLen:LongWord;<br>begin<br> FilePos:=FileSeek(iFileHandle,0,1); //保存文件当前指针位置<br> RecLen:=Length(Str);<br> Result:=(FileWrite(iFileHandle, RecLen, SizeOf(RecLen))>-1) and<br> (FileWrite(iFileHandle, Str[1], RecLen)>-1);<br> if Result=False then<br> FileSeek(iFileHandle,FilePos,0);<br>end;<br><br><br>function AddFileToPackage(SouFile,DestFile:string;RootPath:string=''):boolean;<br>var iFileHandle:integer;<br> FileInfo:TCompFileInfo;<br>begin<br> if FileDirExists(SouFile)=False then exit;<br><br> if FileExists(DestFile) then<br> iFileHandle:=FileOpen(DestFile,fmOpenWrite)<br> else<br> iFileHandle:=FileCreate(DestFile);<br> try<br> FileSeek(iFileHandle,0,2);<br> if RootPath='' then<br> FileInfo.Name:=ExtractFileName(SouFile)<br> else<br> FileInfo.Name:=RightStr(SouFile, Length(SouFile)-Length(RootPath));<br><br> FileInfo.Attr:=IntToStr(FileGetAttr(SouFile));<br> if DirectoryExists(SouFile) then begin<br> FileInfo.Name:=IncludeTrailingPathDelimiter(Fileinfo.Name);<br> FileInfo.Content:='';<br> end<br> else<br> CompressFile(SouFile,FileInfo.Content);<br><br> File_AddString(iFileHandle,FileInfo.Name);<br> File_AddString(iFileHandle,FileInfo.Attr);<br> File_AddString(iFileHandle,FileInfo.Content);<br> finally<br> FileClose(iFileHandle);<br> end;<br>end;<br><br><br>调用<br><br> AddFileToPackage('e:/我的程序/djy/ksxt/CurPaper','e:/我的程序/djy/cs/install/client.z');<br> AddDirToPackage('e:/我的程序/djy/ksxt/TEST','e:/我的程序/djy/cs/install/client.z');<br>