Experience in Delphi compression and decompression of multiple files based on memory flow

Source: Internet
Author: User

There is a problem that needs to be addressed today at work: compressing and decompressing a file, where all the processes are based on memory. For this reason, basic compression work, such as ziplib, cannot be used. Then I found this piece of code from the Internet.

Procedure Compressit (var compressedstream:tmemorystream; const compressionlevel:tcompressionlevel); The parameters are the passed stream and the compression method Var Sourcestream:tcompressionstream; Deststream:tmemorystream; Count:int64; Note that the original size of the int begin//Get stream is modified here, Count: = Compressedstream.size; Deststream: = tmemorystream.create; Sourcestream: = Tcompressionstream.create (CompressionLevel, Deststream); The original stream Compressedstream.savetostream (Sourcestream) is preserved in try//sourcestream; The original stream is compressed, and the compressed flow sourcestream.free is preserved in the Deststream; Compressedstream.clear; The size of the original image is written Compressedstream.writebuffer (count, SizeOf (count)); Writes the compressed stream Compressedstream.copyfrom (deststream, 0); Finally Deststream.free; End End Procedure Uncompressit (const compressedstream:tmemorystream; var uncompressedstream:tmemorystream); The parameters are compressed over the stream, and the stream after decompression is Var Sourcestream:tdecompressionstream; Deststream:tmemorystream; Buffer:pchar; Count:int64; Begin//read from the compressed image stream the original size Compressedstream.readbuffer (count, SizeOf (count)); Allocates a memory block Getmem (Buffer) based on the size of the original stream that will be read in. Count); Deststream: = tmemorystream.create; Sourcestream: = Tdecompressionstream.create (Compressedstream); Try//unzip the compressed stream and then deposit into the Buffer memory block Sourcestream.readbuffer (buffer^, Count); Save the original stream to the Deststream stream Deststream.writebuffer (buffer^, Count); Deststream.position: = 0; Reset flow pointer//deststream.position: = Length (ver_info); The image Stream Uncompressedstream.loadfromstream (deststream) is loaded from the Deststream stream; Finally Freemem (Buffer); Deststream.free; End End

Later because of the change in requirements, but also based on memory but need to compress and decompress multiple files at once. The above method is not applicable. In this process I search online to zipforg can solve this problem. Then downloaded the Zipforge

function Compressitmultifilebyzipforge (str_needtocompress_filenamelist:tstringlist; str_zip_file_name:string): Boolean; var My_ms, My_dest:tmemorystream; Strzipfile, Str_cur_file_name, str_file_name:string; My_zipforge:tzipforge; I, I_file_count:integer; Is_ok:boolean; Begin Strzipfile: = Str_zip_file_name; My_zipforge: = Tzipforge.create (nil); I_file_count: = Str_needtocompress_filenamelist. Count; My_ms: = tmemorystream.create; My_dest: = tmemorystream.create; IS_OK: = True; Try begin//my_zipforge.filename: = Strzipfile; My_zipforge.inmemory: = true;//If it is based on memory flow is important my_zipforge.openarchive (My_dest, True); If I_file_count = 0 THEN begin IS_OK: = False; End ELSE begin for I: = 0 to I_file_count-1 do begin str_file_name: = Str_needtocompress_filenamelist. Strings[i]; Str_cur_file_name: = Ghidepathprefix + str_file_name; My_ms: = Getmemorystreambyfilename (Str_cur_file_name); If My_ms. Size <> 0 THEN BEGIN My_zipforge.addfromstream (Str_file_name, My_ms, True); End My_ms. Clear; End If My_dest. Size <> 0 THEN begin DM. LionSunMANET.DiskManage.SaveFile (Str_zip_file_name, my_dest); IS_OK: = True; End ELSE begin IS_OK: = False; End End My_ms. Clear; My_dest. Clear; my_zipforge.closearchive; End finally begin My_ms. Free; My_dest. Free; End End End

The code for the

Decompression section is as follows:

function Decompresssinglezipfilebyzipforge (str_needtodecompress_filename:string;str_zip_file_name:string): Boolean; var My_ms, My_dest:tmemorystream; Strzipfile, Str_dest_file_name, str_file_name:string; My_zipforge:tzipforge; Is_ok:boolean; Begin Strzipfile: = Str_zip_file_name; My_zipforge: = Tzipforge.create (nil); My_ms: = tmemorystream.create; My_dest: = tmemorystream.create; IS_OK: = True; The file name of the try Begin/zip file if DM. LionSunMANET.DiskManage.ReadFile (Strzipfile,my_ms) = 1 THEN begin//my_zipforge.filename: = Strzipfile; My_zipforge.inmemory: = true;//key//Open compressed document my_zipforge.openarchive (my_ms,false);//key Str_file_name: = Str_ Needtodecompress_filename; My_zipforge.extracttostream (str_file_name,my_dest); Str_dest_file_name: = Ghidepathprefix + str_file_name; My_dest. Position: = 0; Dm. LionSunMANET.DiskManage.SaveFile (str_dest_file_name,my_dest); My_dest. Clear; Close the compressed document My_ms. Clear; my_zipforge.closearchive; IS_OK: = True; End ELSE begin my_dest. Clear; My_ms. Clear; IS_OK: = False; End End Finally begin My_ms. Free; My_dest. Free; End End Result: = IS_OK; End

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.