• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

COMPRESS/DECOMPRESS

How to compress/decompress  a  file in Java?
0
sivakh
Asked:
sivakh
1 Solution
 
SendohCommented:
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
 
ValeriCommented:
Take a look at ZipFile class (java.util.zip.ZipFile)

Valeri
0
 
mohan_sekarCommented:
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
 
lwinkenbCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now