Solved

COMPRESS/DECOMPRESS

Posted on 2001-08-02
4
671 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
Image decoding from Camera 3 85
Apps blocked by Java 9 79
How to convert String matching to regex in java 4 30
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

813 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

15 Experts available now in Live!

Get 1:1 Help Now