将图像从zip提取到内存delphi

时间:2012-06-13 03:58:20

标签: delphi memory zip jpeg delphi-xe2

我想以某种方式将装有图像的zip文件解压缩到内存中。我真的不关心他们进入什么类型的流,只要我之后可以加载它们。我对流没有那么大的理解,关于这个问题的解释似乎没有详细说明。

基本上,我现在正在做的是将文件解压缩到(getcurrentdir +'\ temp \')。这有效,但不是我想要做的。我会更高兴让jpg最终在内存中,然后能够从内存中读取到TImage.bitmap。

我目前正在使用jclcompresion来处理zips和rars,但正在考虑转回system.zip,因为我真的只需要能够处理zip文件。如果使用jclcompression会更容易,虽然这对我有用。

1 个答案:

答案 0 :(得分:6)

read类的TZipFile方法可以与流

一起使用
procedure Read(FileName: string; out Stream: TStream; out LocalHeader: TZipHeader); overload;
procedure Read(Index: Integer; out Stream: TStream; out LocalHeader: TZipHeader); overload;

从此处可以使用索引或文件名访问压缩文件。

检查此示例,该示例使用TMemoryStream来保存未压缩的数据。

uses
  Vcl.AxCtrls,
  System.Zip;

procedure TForm41.Button1Click(Sender: TObject);
var
  LStream    : TStream;
  LZipFile   : TZipFile;
  LOleGraphic: TOleGraphic;
  LocalHeader: TZipHeader;
begin
  LZipFile := TZipFile.Create;
  try
    //open the compressed file
    LZipFile.Open('C:\Users\Dexter\Desktop\registry.zip', zmRead);
    //create the memory stream
    LStream := TMemoryStream.Create;
    try
      //LZipFile.Read(0, LStream, LocalHeader); you can  use the index of the file
      LZipFile.Read('SAM_0408.JPG', LStream, LocalHeader); //or use the filename 
      //do something with the memory stream
      //now using the TOleGraphic to detect the image type from the stream
      LOleGraphic := TOleGraphic.Create;
      try
         LStream.Position:=0;
         //load the image from the memory stream
         LOleGraphic.LoadFromStream(LStream);
         //load the image into the TImage component
         Image1.Picture.Assign(LOleGraphic);
      finally
        LOleGraphic.Free;
      end;
    finally
      LStream.Free;
    end;
  finally
   LZipFile.Free;
  end;
end;