Solved

how to zip & unzip files using java.

Posted on 2001-09-03
6
627 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
6 Comments
 
LVL 1

Expert Comment

by:olegtim
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Expert Comment

by:realman1
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

763 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

11 Experts available now in Live!

Get 1:1 Help Now