Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to zip & unzip files using java.

Posted on 2001-09-03
6
Medium Priority
?
662 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses

636 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