Juraj
Juraj

Reputation: 2041

Is there any java compression utility

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

Upvotes: 1

Views: 1418

Answers (3)

jar is a disguised unzipper. Is that usable?

Upvotes: 0

VonC
VonC

Reputation: 1324278

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();
      }
   }
}

Upvotes: 4

user136698
user136698

Reputation:

Maybe Compress from Apache Commons could help you.

Upvotes: 2

Related Questions