?
Solved

COMPRESS/DECOMPRESS

Posted on 2001-08-02
4
Medium Priority
?
677 Views
Last Modified: 2008-03-03
How to compress/decompress  a  file in Java?
0
Comment
Question by:sivakh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
Sendoh earned 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 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