Solved

COMPRESS/DECOMPRESS

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

17 Experts available now in Live!

Get 1:1 Help Now