Solved

COMPRESS/DECOMPRESS

Posted on 2001-08-02
4
670 Views
Last Modified: 2008-03-03
How to compress/decompress  a  file in Java?
0
Comment
Question by:sivakh
4 Comments
 
LVL 3

Accepted Solution

by:
Sendoh earned 100 total points
ID: 6347119
hi !
for simpler version of compression can check out
http://patriot.net/~tvalesky/zip.html

and more detail version can check out...
http://www.j-nine.com/pubs/javazip/Java_Zip.html

hope it helps...

regards
Sendoh
0
 
LVL 16

Expert Comment

by:Valeri
ID: 6347807
Take a look at ZipFile class (java.util.zip.ZipFile)

Valeri
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6348649
Hi,

      Here is the sample code to do it

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

public class ZipCompress
{
     public static void main(String[] args)
     {
         try
         {
              FileOutputStream f = new FileOutputStream("test.zip");
                CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
                ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(csum));
                out.setComment("A test of Java Zipping");

                for(int i = 0; i < args.length; i++)
                {
                     System.out.println("Writing file " + args[i]);
                  BufferedReader in = new BufferedReader(new FileReader(args[i]));
                  out.putNextEntry(new ZipEntry(args[i]));
                  int c;
                  while((c = in.read()) != -1)
                       out.write(c);
                  in.close();
                }
           out.close();
           System.out.println("Checksum: " +
        csum.getChecksum().getValue());

           System.out.println("Reading file");
           FileInputStream fi = new FileInputStream("test.zip");
           CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
           ZipInputStream in2 = new ZipInputStream(new BufferedInputStream(csumi));
           ZipEntry ze;
           System.out.println("Checksum: " + csumi.getChecksum().getValue());
           while((ze = in2.getNextEntry()) != null)
           {
             System.out.println("Reading file " + ze);
             int x;
             while((x = in2.read()) != -1)
                  System.out.write(x);
           }
           in2.close();

         //2nd method to read a .zip file
         ZipFile zf = new ZipFile("test.zip");
           Enumeration e = zf.entries();
           while(e.hasMoreElements()) {
             ZipEntry ze2 = (ZipEntry)e.nextElement();
        System.out.println("File: " + ze2);
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
}

bye
:-)

Mohan
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 8895401
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept comment by Sendoh as answer.
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
lwinkenb
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now