Data compression - application

xiaoxiao2021-03-06  58

Example: Multiple files are compressed into a file in multiple files; extract the compressed files to a corresponding directory. // Compressed file stream: File name length file name file length compressed stream Uses lh5Unit.pas; ​​// see data compression - source code procedure compress; var foutstr: tFileStream; // Compressed file stream

Function doonefile (srcfile: string): boolean; // Compress a file to compressed file stream VAR Finstr: TFileStream; ftmp: tmemorystream; fnlen, sz: integer; begin result: = true; if not fileexists (srcfile). Try Finstr: = TFileStream.create (srcfile, fmopenread); ftmp: = tmemorystream.create; try // Insert file name length, file name, file length fnlen: = length (srcfile); foutstr.write (fnlen, SIZEOF (Integer); // File Name Length / / Or SIZEOF (i) FoutStr.write (PfileName [1], Fnlen); // File Name Lhacompress (Finstr, FTMP); // Compressed file to TMEMORYSTREAM SZ: = FTMP.SIZE; FoutStr.Write (SZ, SIZEOF (Integer)); // File compression length foutstr.write (ftmp.memory ^, sz); // Compressed Finally FinStr.Free; ftmp.free; end; Except Result : = false; End;

Var lhfile, AfileName: string; begin result: = true; trypathen (Application.exename) 'FilePack.LHz'; // Compressed file name if FileExists (lhfile); lhfile;

Foutstr: = TFileStream.create (LHFILE, FMCREATE); TRY ..... // Retrieves the list of files to be compressed OpenSQL ('SELECT HTMFILE from FAQ WHERE FLAGS = 1 Order By HTMFile', Data.TBLHz);

while not data.tbLHZ.eof do begin aFileName:. = 'FAQfile /' data.tbLHZ.FieldByname ( 'HtmFile') asString '. html'; if not doOneFile (ExtractFilePath (application.ExeName) aFileName, aFileName) then Begin Result: = false; // Compressed unsuccessful Break; end; data.tblhz.next; end; finally foutstr.free; end; except results: = false; // Compressed unsuccessful end; end; function expand (lhfile: String): boolean; var src_f: tfilestream;

function getOneFile (aFileLen: integer; tFileName: string): boolean; var dst_f: Tfilestream; Mem_f: TmemoryStream; begin result: = true; try if fileExists (tFileName) then deletefile (aFile); // already exists, it is covered

dst_f: = Tfilestream.create (aFile, fmcreate or fmopenwrite); Mem_f: = TmemoryStream.create; try if Mem_f.CopyFrom (src_f, aFileLen) <> aFileLen then raiselastWin32Error; // Get the compressed stream Mem_f.position: = 0;

Lhaexpand (MEM_F, DST_F); // Unzip

Finally dst_f.free; MEM_F.FREE; END; EXCEPT RESULT: = FALSE; END;

VAR AFILENAME: STRING; FNLEN, FSIZE: Inteder; Begin if not fileexists (lhfile) THEN EXIT; // Compressed file does not exist! FilePack.LHz

Result: = true; try src_f: = tfilestream.create (lhfile, fmopenread); // Separate all files from the temporary file: file name length file name file compression length compressed flow (Pressing file) TRY SRC_F.POSITION: = 0; While True Do Begin if Src_f.size <= src_f.position 1 Then Break; // (2.0) If IRTN <= 0 file flow reading end IF SRC_F. Read (Fnlen, Sizeof (Integer) <= 0 THEN BREAK; / / (2.1) acquired file name length setlength; if src_f.read (AfileName [1], fnlen) <= 0 THEN BREAK; / /(2.2) Name IF SRC_F.READ (FSIZE, SIZEOF (Integer) <= 0 THEN BREAK; / / (2.3) Get Compression Length If getonefile (fsize, afilename) Then // (2.4) Get compressed file Begin FRMMSG.MOMSGS.LINES.ADD (AfileName 'decompression success!'); frmmsg.Update; end else begin frmmsg.momsgs.Lines.add (AfileName 'decompression is unsuccessful!'); frMmsg.Update; end; end; Finally SRC_F.Free; End; Except Result: = FALSE; END;

转载请注明原文地址:https://www.9cbs.com/read-109132.html

New Post(0)