views:

234

answers:

3

I need to (un)zip some files and after some googling I haven't found any utility for doing it. I know I can do it with built-in java.uitl.zip.* classes but isn't there any utility, that will make it easer, best like this:

SomeClass.unzip("file_name.zip", "target_directory");

or

SomeClass.zip("source_directory", "target_file_name.zip", recursively);

I don't want to handle streams. Just file, or better just file names...

A: 

Maybe Compress from Apache Commons could help you.

Altherac
What I don't like at this is that I always have to handle streams. I wnat it realy simple to use. The only thing to handle are files, in fact names of files...
Supowski
+2  A: 

How about the Deflater/Inflater classes mentioned in the question "What’s a good compression library for Java?".

I know the current interfaces proposed by Java are Stream-based, not "filename"-based, but according to the following article on Java compression, it is easy enough to build an utility class around that:

For instance, a Unzip function based on a filename would look like:

import java.io.*;
import java.util.zip.*;

public class UnZip {
   final int BUFFER = 2048;
   public static void main (String argv[]) {
      try {
         BufferedOutputStream dest = null;
         FileInputStream fis = new 
       FileInputStream(argv[0]);
         ZipInputStream zis = new 
       ZipInputStream(new BufferedInputStream(fis));
         ZipEntry entry;
         while((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " +entry);
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new 
          FileOutputStream(entry.getName());
            dest = new 
              BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) 
              != -1) {
               dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
         }
         zis.close();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}
VonC
I'm looking for something simpler. No streams, maximally files, in fact file names...
Supowski
As I just need to extract one zipe into temp directory (extracted files will be processed afterwards by other logic) I made something similar. I'm just wondering that there isn't any simpler way because I think almost everybody who want to extract zip had to wrote something similar. After so many years in java world I always get surprised that simple things can be done complex and complicate :) Anyway, thx for answer...
Supowski
you must be new to java
jim
A: 

jar is a disguised unzipper. Is that usable?

Thorbjørn Ravn Andersen
It works with streams too...
Supowski