簡體   English   中英

Zip 文件的內存壓縮

[英]In-memory compression of a Zip file

我想將文件壓縮到內存中的 Zip(我會將結果存儲在數據庫中,而不是在文件系統中創建文件)。

如何讀取壓縮的 Zip 文件的原始內容? 我看不到任何屬性來讀取該內容,也看不到將該內容保存到 Stream 或任何其他內存結構中的任何方法。

function GetZip(UncompressedFile: TStream): TBytes; 
var AZipFile: TZipFile;
begin
  AZipFile := TZipFile.Create;
  try
    AZipFile.Add(UncompressedFile);
    Result := AZipFile.Data;  // ?? How to get the compressed file without saving it to the file system ??
  finally
    AZipfile.Free;
  end;
end;

謝謝你。

感謝 AmigoJack 和 Remy Lebeau 讓我知道我可以使用 Zip 輸入 Stream 來獲得結果。

這很好用:

function Zip_AddFile(FileToAdd: TStream; Title: string): TBytes; overload; 
begin
   Result := Zip_AddFile([], FileToAdd, Title);
end;

function Zip_AddFile(Zip: TBytes; FileToAdd: TStream; Title: string): TBytes; overload;
var AZipFile: TZipFile;
    ZipStream: TBytesStream;
begin
  ZipStream := TBytesStream.Create(Zip);
  AZipFile := TZipFile.Create;
  try
    AZipFile.Open(ZipStream, zmReadWrite);
    AZipFile.Add(FileToAdd, Title);
    AZipFile.Close;
    Result := ZipStream.Bytes;
  finally
    AZipfile.Free;
    ZipStream.Free;
  end;
end;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM