1

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...

Juraj
  • 1,951
  • 2
  • 21
  • 30

3 Answers3

4

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();
      }
   }
}
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I'm looking for something simpler. No streams, maximally files, in fact file names... – Juraj Aug 06 '09 at 11:21
  • 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... – Juraj Aug 06 '09 at 11:43
  • @jim I know that java doesn't do the think in easy way.. Anyway it look like this is the best possible solution -> accepting it – Juraj Feb 15 '11 at 10:31
  • "always get surprised that simple things can be done complex and complicate" was what I was referring to. just a java joke no offense intended. – lsl Feb 28 '11 at 21:31
2

Maybe Compress from Apache Commons could help you.

christopheml
  • 2,444
  • 17
  • 25
  • 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... – Juraj Aug 06 '09 at 11:03
0

jar is a disguised unzipper. Is that usable?

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347