Advertisement

04.15.2008 at 11:42AM PDT, ID: 23324772
[x]
Attachment Details

How to use java.util.zip package

Asked by lkettere in File Compression Utilities, Java Standard Edition

I need to develop an application that, among other functions, is able to programmatically compress a large number of document files for storage and retrieval in a database.  As a first step, I've written a small java program using the java.util.zip package to see how much these files can be compressed.  The program (listing attached) simply zips all the files in a given directory to a zip file.  It seems to work, but produces almost zero compression for any of the file types I've tried, txt, doc, xml, pdf, xls, html, java, ppt, and even gif.  Am I missing something in this program?  I was expecting to see compression ratios of 50% or more on most files.  Is that realistic, and if so, how can it be done in java?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
 
 
public class Compress {
 
  /** Zip the contents of the directory, and save it in the zipfile */
  public static void zipDirectory(String dir, String zipfile)
      throws IOException, IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
      throw new IllegalArgumentException("Not a directory:  "
          + dir);
    String[] entries = d.list();
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;
 
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
    out.setMethod(Deflater.DEFLATED);
    out.setLevel(Deflater.BEST_COMPRESSION);
 
    for (int i = 0; i < entries.length; i++) {
      File f = new File(d, entries[i]);
      if (f.isDirectory())
        continue;//Ignore directory
      FileInputStream in = new FileInputStream(f); // Stream to read file
      ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
      out.putNextEntry(entry); // Store entry
      while ((bytesRead = in.read(buffer)) != -1)
        out.write(buffer, 0, bytesRead);
      in.close();
    }
    out.close();
  }
 
  public static void main(String args[]) throws IOException {
	String dir = "c:/test/data";
    Compress.zipDirectory(dir,dir + ".zip");
  }
}
[+][-]04.15.2008 at 12:24PM PDT, ID: 21361927

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.15.2008 at 12:41PM PDT, ID: 21362098

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.15.2008 at 12:49PM PDT, ID: 21362181

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: File Compression Utilities, Java Standard Edition
Sign Up Now!
Solution Provided By: contactkarthi
Participating Experts: 1
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628