Solved

how to zip & unzip files using java.

Posted on 2001-09-03
6
636 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
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 50 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Basic Java Case or If-Else statement... 3 50
nextBoolean(double p) for Random class 3 40
login jsp example 24 50
check java version using powershell 13 92
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

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