[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 668
  • Last Modified:

how to zip & unzip files using java.

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
Chintamani
Asked:
Chintamani
1 Solution
 
olegtimCommented:
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
 
anhphuongnnCommented:
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
 
realman1Commented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
realman1Commented:
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
 
vemulCommented:
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
 
SpideyModCommented:
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now