2012-06-13 37 views
1

我想要以某种方式提取加载图像到内存的zip文件。我并不在乎它们进入哪种类型的流,只要我可以在之后加载它们。我对溪流的理解并不那么深入,关于这个问题的解释似乎没有详细说明。提取图像从zip到内存delphi

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

我目前使用jclcompresion来处理拉链和rars,但正在考虑搬回system.zip,因为我真的只需要能够处理zip文件。如果使用jclcompression更容易,尽管这对我很有用。

回答

6

TZipFile类的read方法可以用流

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; 
+0

我正确的假设索引编制完成0,1,2,等每个增量是下一个文件吗?如果是这样,这比我想象的要容易得多。 – Larmos

+0

是索引是从零开始的,并且最大值必须使用TZipFile类的'FileCount'属性获得。 – RRUZ

+0

非常感谢。我明天会实施这个。 – Larmos