Given
- URL of an archive (e.g. a zip file)
- Full name (including path) of a file inside that archive
I’m looking for a way (preferably in Java) to create a local copy of that file, without downloading the entire archive first.
From my (limited) understanding it should be possible, though I have no idea how to do that. I’ve been using TrueZip, since it seems to support a large variety of archive types, but I have doubts about its ability to work in such a way. Does anyone have any experience with that sort of thing?
EDIT: being able to also do that with tarballs and zipped tarballs is also important for me.
Well, at a minimum, you have to download the portion of the archive up to and including the compressed data of the file you want to extract. That suggests the following solution: open a
URLConnectionto the archive, get its input stream, wrap it in aZipInputStream, and repeatedly callgetNextEntry()andcloseEntry()to iterate through all the entries in the file until you reach the one you want. Then you can read its data usingZipInputStream.read(...).The Java code would look something like this:
This is, of course, untested.