第一種方法:
用ZLib庫裡的TZCompressionStream、TZDecompressionStream,缺點是只能對單個檔案進行壓縮、壓縮後不能用WinRar這些軟體開啟,例子如下:
壓縮:
TFileStream *input = new TFileStream("E:\\test.wav", fmOpenRead);
TFileStream *output = new TFileStream("E:\\test.zip", fmCreate);
// 注意各個RAD XE版本裡的TZCompressionStream的建構函式有區別
// TZCompressionStream *zip = new TZCompressionStream(output);
TZCompressionStream *zip = new TZCompressionStream(TCompressionLevel::clMax, output);
zip->CopyFrom(input, input->Size);
delete zip;
delete input;
delete output;
解壓:
TFileStream *input = new TFileStream("E:\\test.zip", fmOpenRead);
TFileStream *output = new TFileStream("E:\\test2.wav", fmCreate);
TZDecompressionStream *unzip = new TZDecompressionStream(input);
output->CopyFrom(unzip, 0);
delete unzip;
delete input;
delete output;
第二種方法:
用TZipFile類,預設只能用TZipCompression::zcStored、TZipCompression::zcDeflate這2種模式,TZipCompression的其它模式可能要註冊自己寫的回呼函數
壓縮整個目錄:
TZipFile::ZipDirectoryContents("E:\\Log1.zip", "E:\\Tmp\\LOG");
壓縮單個或多個檔案:
String ZipFile = "E:\\Log.zip";
String File1 = "E:\\Tmp\\LOG\\file1.log";
String File2 = "E:\\Tmp\\LOG\\file2.log";
TZipFile * zip = new TZipFile();
zip->Open(ZipFile, TZipMode::zmWrite);
zip->Add(File1, "log1.log");
zip->Add(File2, "log2.log");
zip->Close();
delete zip;
解壓全部檔案:
使用TZipFile的ExtractAll函數
解壓單個檔案:
使用TZipFile的Extract函數