skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

Java Zip file utilities

Posted by Admin at 9:26 AM – 0 comments
 

The java code provides various utility functions pertaining to Zipping and Compressing .


import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.zip.GZIPOutputStream;

import java.io.IOException;

public class ZipFileUtil {

   public ZipOutputStream createZipFile(String outFile,String[] files) {

        String zipFileName = outFile;
        byte[] buf = new byte[1024];

        try {

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));

            System.out.println("Archive:  " + zipFileName);

            // Compress the files
            for (int i=1; i<files.length; i++) {

FileInputStream in = new FileInputStream(files[i]);
                
                out.putNextEntry(new ZipEntry(files[i]));

                // Transfer bytes from the file to the ZIP file
                int len;
                while((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();

            }

            // Complete the ZIP file
            out.close();

    //Return Zip file
     return out; 

        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

   public void unzipFiles(String zipFileName) {

        try {

            ZipFile zf = new ZipFile(zipFileName);

            System.out.println("Archive:  " + zipFileName);
            
            // Enumerate each entry
            for (Enumeration entries = zf.entries(); 
entries.hasMoreElements();) {
                
                // Get the entry and its name
                ZipEntry zipEntry = 
(ZipEntry)entries.nextElement();
                String zipEntryName = zipEntry.getName();
                
                int lastDirSep;
           if ( (lastDirSep = zipEntryName.lastIndexOf('/'))>0) {
          String dirName = zipEntryName.substring(0, lastDirSep);
                    (new File(dirName)).mkdirs();
                }
                
                
                if (!zipEntryName.endsWith("/")) {
                    OutputStream out = new 
    FileOutputStream(zipEntryName);
                    InputStream in = zf.getInputStream(zipEntry);
                    
                    byte[] buf = new byte[1024];
                    int len;
                    while((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
    
                    // Close streams
                    out.close();
                    in.close();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }

  public void listFiles(String zipFileName) {

        try {

           System.out.println("Opening zip file " + zipFileName);
            ZipFile zf = new ZipFile(zipFileName);

            int counter = 0;

            // Enumerate each entry
        for (Enumeration entries = zf.entries(); 
entries.hasMoreElements();) {

                counter++;
                
                // Get the entry name
                String zipEntryName = 
((ZipEntry)entries.nextElement()).getName();
              }

        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }

   public GZIPOutputStream compressFile(String inFileName) {

        try {
        
          System.out.println("Creating the GZIP output stream.");
            String outFileName = inFileName + ".gz";
            GZIPOutputStream out = null;
            try {
                out = new GZIPOutputStream(new FileOutputStream(outFileName));
            } catch(FileNotFoundException e) {
             System.err.println("Could not create file: " + 
outFileName);
                System.exit(1);
            }
                    

            FileInputStream in = null;
            try {
                in = new FileInputStream(inFileName);
            } catch (FileNotFoundException e) {
             System.err.println("File not found. " + inFileName);
                System.exit(1);
            }

            //Transfering bytes from input file to GZIP Format
            byte[] buf = new byte[1024];
            int len;
            while((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();

            System.out.println("Completing the GZIP file");
            out.finish();
            out.close();
     return out;
        
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }

  public FileOutputStream uncompressFile(String inFileName) {

        try {

            if (!getExtension(inFileName).equalsIgnoreCase("gz")) {
                System.err.println("File name must have extension 
of ".gz"");
                System.exit(1);
            }

             GZIPInputStream in = null;
            try {
                in = new GZIPInputStream(new FileInputStream(inFileName));
            } catch(FileNotFoundException e) {
                System.err.println("File not found. " + 
inFileName);
                System.exit(1);
            }

            System.out.println("Open the output file.");
            String outFileName = getFileName(inFileName);
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(outFileName);
            } catch (FileNotFoundException e) {
                System.err.println("Could not write to file. " + 
outFileName);
                System.exit(1);
            }

//Transfering bytes from compressed file to the output file
            byte[] buf = new byte[1024];
            int len;
            while((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

            System.out.println("Closing the file and stream");
            in.close();
            out.close();
     return out;
        
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }

    }


Labels: Zip File

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger