java zip program

Sample Java Programs

java zip program

Postby admin » Wed Nov 22, 2006 5:05 pm

Code: Select all
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZipFile {
  public static int BUFFER_SIZE = 10240;
        public static void main(String[] args) {
                if (args.length != 2) {
                        System.out.println("Usage: java makeZipFile " +
                                " [files to be zipped] [filename after zip] ");
                        return;
                }
        try {
            String filename = args[0];
            String zipfilename = args[1];
            CreateZipFile list = new CreateZipFile();
            //makeZipFile list = new makeZipFile( );
            list.createZipArchive(new File(filename), new File(zipfilename));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


  protected void createZipArchive(File archiveFile, File tobeZippedFiles) {
    try {
      byte buffer[] = new byte[BUFFER_SIZE];
      // Open archive file
      FileOutputStream stream = new FileOutputStream(archiveFile);
      ZipOutputStream out = new ZipOutputStream(stream);

        System.out.println("Adding " + tobeZippedFiles.getName());

        // Add archive entry
        ZipEntry zipAdd = new ZipEntry(tobeZippedFiles.getName());
        zipAdd.setTime(tobeZippedFiles.lastModified());
        out.putNextEntry(zipAdd);

        // Read input & write to output
        FileInputStream in = new FileInputStream(tobeZippedFiles);
        while (true) {
          int nRead = in.read(buffer, 0, buffer.length);
          if (nRead <= 0)
            break;
          out.write(buffer, 0, nRead);
        }
        in.close();

      out.close();
      stream.close();
      System.out.println("Adding completed OK");
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Error: " + e.getMessage());
    }
   }
}


usage:

Code: Select all
/opt/java1.3/bin/java -cp .  CreateZipFile abc.zip testfile
admin
Site Admin
 
Posts: 552
Joined: Mon Jan 02, 2006 1:31 pm

Re: java zip program

Postby admin » Sat Nov 29, 2008 1:54 pm

Code: Select all
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

public class ZipCreate {
    public static void main(String args[]) {
        if (args.length != 2) {
            System.out
                    .println("Usage: java makeZipFile "
                            + "  [filename after zip] [file has list of files to be zipped]");
            System.exit(1);
        }
        try {

            String zipfilename = args[0];
            String filename = args[1];
            File f = new File(filename);
            if ( ! f.exists() || ! f.canRead() ){
                System.out.println("File Not exists or uanble to  read : " + filename);
                System.out.println("Exiting..");
                System.exit(-1);
            }
            ArrayList filelist = new ArrayList();

            FileReader fr = new FileReader(filename);
            BufferedReader br = new BufferedReader(fr);

            String record = new String();
            int recCount = 0;
            while ((record = br.readLine()) != null) {
                filelist.add(record);
                System.out.println(recCount + ": " + record);
                recCount++;

            }
            if( 0 == recCount ){
                System.out.println("Input File is Empty");
                System.out.println("Exiting..");
                System.exit(-1);
            }
            String[] fileslist = new String[filelist.size()];
            filelist.toArray(fileslist);

            ZipCreate zc = new ZipCreate();
            zc.create(zipfilename, fileslist);
            System.out.println("Zip file Created: " + zipfilename);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void create(String zipfilename, String filename[]) {
        byte block[] = new byte[256];
        int length;
        try {
            FileOutputStream fos = new FileOutputStream(zipfilename);
            ZipOutputStream out = new ZipOutputStream(fos);
            for (int i = 0; i < filename.length; i++) {
                File f = new File(filename[i]);
                if (f.isDirectory()) {
                    // if its direcotry skip it.
                    // loop again
                    System.out.println("Direcotry skipping:"+ filename[i]);
                    continue;
                }
               
                FileInputStream in = new FileInputStream(filename[i]);
                String name = filename[i].replace(File.separatorChar, '/');
                ZipEntry zipentry = new ZipEntry(name);
                out.putNextEntry(zipentry);
                while ((length = in.read(block)) > 0)
                    out.write(block, 0, length);
                out.closeEntry();
                in.close();
            }
            out.close();
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}



Run this program like this:

Code: Select all
java -cp .  CreateZipFile abc.zip filelists.txt


or
Code: Select all
java -jar createzipfile.jar   CreateZipFile abc.zip filelists.txt

PS: filelists.txt will have list of files to zip.
admin
Site Admin
 
Posts: 552
Joined: Mon Jan 02, 2006 1:31 pm


Return to Java Progarams

Who is online

Users browsing this forum: No registered users and 1 guest

cron