5 Replies Latest reply: Jan 9, 2008 4:48 PM by 807603 RSS

    Java and Zip

      Is there a way to extract a specified file from an archive without having to decompress the entire archive?

      From what I've found, when files are zipped, there aren't boundaries between the files, therefore not easy to grab a single file.
        • 1. Re: Java and Zip
          open it with winzip and extract a single file?
          • 2. Re: Java and Zip
            In Java have a look at ZipFile. With that you can get a single file from a zip file. It only decompresses the needed file.
            • 3. Re: Java and Zip
              Just to expand on twupak's response, within the ZipFile object you'll find one or more ZipEntry objects. If you know the name of the file you want to extract, you can use the ZipFile's getEntry(String name) method to move to the desired entry (an example for extracting the file after locating it within the archive is given below). If you're not sure of the exact name (say you wanted to extract all .txt files for example) you can always loop through all the entries (using the aptly named entries() method). Note that you can also use a ZipInputStream object to retrieve entries.

              Below are a couple good examples from the Java Developer's Almanac site that you may find useful. Neither example is exactly what you're looking for but between them should give you enough information to locate and extract the file(s) you want.
              [url http://javaalmanac.com/egs/java.util.zip/ListZip.html?l=rel]Listing the Contents of a Zip file
              [url http://javaalmanac.com/egs/java.util.zip/GetZip.html?l=rel]Retrieving a Compressed File from a Zip File

              Also check out the documentation for the [url http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/package-frame.html]java.util.zip package, which may give you additional ideas on tackling your particular needs.

              Good Luck,
              ∞ brewman ∞
              • 4. Re: Java and Zip
                java -xf my.jar myfilename
                • 5. Re: Java and Zip
                  mikemian wrote:
                  java -xf my.jar myfilename
                  You don't think that after 39 months and 2 weeks this advice might be a little late? Just a tad? Maybe?