Are there any zip components with such features? I need to download a zip archive from the Internet to a stream, then to open the archive from the stream and then to extract files to another stream.
E.g. ZipForge can open an archive from a stream ZipForge.OpenArchive(MyStream, false);
but how to extract to another one…?
procedure ExtractToStream(FileName: WideString; Stream: TStream);
Description
Use ExtractToStream to decompress data stored in the file inside the
archive to a TStream descendant object like TFileStream, TMemoryStream
or TBlobStream.The FileName parameter specifies file name being extracted.
And what use of the OpenArchive(MyStream, false)
method if extraction isn't supported…
Best Answer
The zip file component that is built into XE2 will do this.
There is an overloaded
Open
method that receives aTStream
as its input parameters.To extract individual files you can call an overloaded
Read
method passing the name of the file that you wish to extract. The extracted file is returned as a new instance ofTStream
. You can that useCopyFrom
on that instance to transfer the extracted file to your stream.Note that I've not tested this code, I've just written it based on the source code for
TZipFile
and the documentation contained in that source code. There may be a few wrinkles in this but if the code behaves as advertised it meets your needs perfectly.OK, now I tested it because I was curious. Here's the program that shows that this all works as advertised:
Note that I extracted by index rather than file name since that was more convenient for me. And I used file streams rather than memory streams which I imagine you would use. However, since the
TZipFile
methods work withTStream
I'm sure that the code will work with streams of any form.This is the latest in a series of questions about ZIP files. I know that you are using XE2 and I wonder why you seem reluctant to use the built in ZIP class that XE2 provides. I've not seen anything to indicate that it will not fulfil your requirements. In fact, it is precisely this ability to work directly with streams that makes me feel it has sufficient generality for any application.