?
Solved

how to zip & unzip files using java.

Posted on 2001-09-03
6
Medium Priority
?
655 Views
Last Modified: 2008-02-01
Hi all,

I have to write a java application which zip files from the local m/c and transfers the .zip  files onto the server.Again on the server the reverse procedutre is to be done i.e unzipping and storing it on the m/c.
My problem is that I am not able to understand how to zip and unzip files through java code.
I would be very gratefull if any body could let me know the procedure of zipping and unzipping files ...if possible with a sample program.

with regards &  thanks

chintamani.
0
Comment
Question by:Chintamani
[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
6 Comments
 
LVL 1

Expert Comment

by:olegtim
ID: 6452289
Here is code sample and some comments for zipping and sending files, hope it helps you.

http://www.javaside.com/asp/mus.asp?page=/us/servlet/zipservlet.shtml

Sincerely,
Oleg
0
 

Accepted Solution

by:
anhphuongnn earned 200 total points
ID: 6452336
Hi,

Try zip file this ex:

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

class test
{

     public static void ZipFile(String cfgFile) {

      ZipOutputStream a_ZipOutStream = null;
       File outFilename = new File(cfgFile);
       int len;
       try{
          // Create a zip output stream
          a_ZipOutStream = new ZipOutputStream( new FileOutputStream(outFilename.getName()+".zip"));
          byte[] buf = new byte[1024];

       File oneFile = new File(cfgFile);
       if(oneFile.isFile()){
               ZipEntry ze = new ZipEntry(cfgFile);
               FileInputStream is = new FileInputStream(oneFile);
               len = is.read(buf);
               a_ZipOutStream.putNextEntry(ze);
               ze.setTime(oneFile.lastModified());
               a_ZipOutStream.write(buf,0,len);
               a_ZipOutStream.closeEntry();
               System.out.println(ze.getName() +" (" + ze.getCompressedSize()*100/ze.getSize() +"%)");
               is.close();
          }
      }
      catch(IOException ioe)
      {
      }
      finally
      {
        if(a_ZipOutStream != null)
        {
           try{
           a_ZipOutStream.close();
           }
           catch(IOException ioe)
           {
               ioe.printStackTrace();
           }
           a_ZipOutStream = null;
        }
      }
     }


     public static void main (String agrs[])
     {
          ZipFile("test.java");
     }


Best regard
PhuongNguyen
0
 

Expert Comment

by:realman1
ID: 6993248
0
Industry Leaders: 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!

 

Expert Comment

by:realman1
ID: 6993250
import java.io.*;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipEntry;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.ZipOutputStream;

public class ZipFile {          
      public void Zip(String strFile, String ZipFileName) throws Exception {                    
     
                FileOutputStream fout = new FileOutputStream(ZipFileName);    
                ZipOutputStream zipoutputstream = new ZipOutputStream(fout);

                zipoutputstream.setMethod(ZipOutputStream.DEFLATED);                            
                CRC32 crc32 = new CRC32();                
                File file = new File(strFile);
               
                FileInputStream fileinputstream = new FileInputStream(file);              
                int x = fileinputstream.available();
                byte [] rgb = new byte [x];
                int n;
                while ((n = fileinputstream.read(rgb)) > -1) {
                        crc32.update(rgb, 0, n);
                }

                fileinputstream.close();      
                ZipEntry zipentry = new ZipEntry(strFile);
                zipentry.setSize(file.length());
                zipentry.setTime(file.lastModified());
                zipentry.setCrc(crc32.getValue());
                                       
                // Add the ZIP entry and associated data.
                zipoutputstream.putNextEntry(zipentry);
                fileinputstream = new FileInputStream(file);
                while ((n = fileinputstream.read(rgb)) > -1) {
                        zipoutputstream.write(rgb, 0, n);
                }

                fileinputstream.close();
                zipoutputstream.closeEntry();                
                zipoutputstream.close();
        }
                       
        public void Unzip(String ZipFileName,String OutPath)throws Exception {
                String str,strFileName;
               
                //OutPath = Extract to...
                FileInputStream inStream = new FileInputStream(ZipFileName);            
                ZipInputStream zipinputstream = new ZipInputStream(inStream);
                   
                while (true) {    
                        ZipEntry zipentry = zipinputstream.getNextEntry();

                        if (zipentry == null)
                                   break;                      
                        str = zipentry.getName();
                                             
                        File file = new File(str);
                        strFileName = file.getName();                        
               
                        int x = inStream.available();
                        byte [] rgb = new byte [x];                              
                        int n;
     
                        FileOutputStream fout = new FileOutputStream(OutPath);
                        while ((n = zipinputstream.read(rgb)) > -1) {      
                                fout.write(rgb, 0, n);                    
                        }
                        zipinputstream.closeEntry();
                           
                        fout.close();
                }
               
                zipinputstream.close();
                inStream.close();                
        }            
         
        public static void main(String args[]) {
               ZipFile testZip = new ZipFile();
               try
               {
                    testZip.Zip("inputFile", "output.zip");
                    testZip.Unzip("output.zip","outputFile");                    
               }
               catch (Exception e1)
               {
                    e1.printStackTrace();
               }
          }
}
0
 
LVL 5

Expert Comment

by:vemul
ID: 7765989
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:

- split points between anhphuongnn and realman1

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7847412
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange

realman1 points for you at:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20489659.html
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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
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 …
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 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