Results 1 to 4 of 4
  1. #1
    kribs is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default Java API to ZIP a file

    i tried zipping a file using some java API but found some issues like file size to get zipped restrictions. Can anyone help me out / worked on resolving,

    how to zip a single file of indefinite size using a java code?

    Thanks for your time and inputs.

    Regards,
    kribs

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    kribs is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    ---------------------------------------
    ths s d sample code which i used to zip a file.
    ---------------------------------------
    import java.io.*;
    import java.util.zip.*;

    class makeZipFile {
    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];
    makeZipFile list = new makeZipFile( );
    list.doZip(filename,zipfilename);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public void doZip(String filename,String zipfilename) {
    try {
    long[] buf = new long[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf,0,buf.length);
    System.out.println("cvbxcvbxcvbbuf"+buf.length);


    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream)new FileOutputStream(zipfilename));

    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long)buf.length);
    crc.reset();
    crc.update(buf);
    entry.setCrc( crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I don't normally do this, but I wanted to play around with zip stuff anyway because I need it for a project I'm getting ready to work, so I just wrote your app for you. Enjoy.

    Java Code:
    import java.io.*;
    import java.util.*;
    import java.util.zip.*;
    
    public class zipit {
    
      // Poor practice to have everything just throw Exception...
    
      // Poor practice to do everything in main 
      public static void main(String[] args) throws Exception {
    
        // no argument checking is also poor practice
        List<String> fileNames = new Vector<String>();
    
        // add all the files you are interested in 
        for(int i=0; i<args.length-1; i++)
          fileNames.add(args[i]);
    
        new zipit().zipItGood(fileNames, args[args.length-1]);
      }
    
      public void zipItGood(List<String> fileNames,String zipfilename) 
        throws Exception 
      {
    
        ZipOutputStream out = 
          new ZipOutputStream(new FileOutputStream(zipfilename));
    
        byte[] buf = new byte[1048576];
    
        for(String thisFile : fileNames)
          addThisFile(thisFile, out, buf);
    
        out.close();
      }
    
      void addThisFile(String fileName, ZipOutputStream out, byte[] buf) 
          throws Exception 
        {
        File inFile = new File(fileName);
        long size = inFile.length();
    
        BufferedInputStream in = 
          new BufferedInputStream(new FileInputStream(inFile));
    
        ZipEntry entry = new ZipEntry(fileName);
        entry.setSize(size);
        out.putNextEntry(entry);
    
        CRC32 crc = new CRC32();
    
        while(true) {
          int numRead = in.read(buf);
    
          if(numRead == -1) break;
    
          crc.update(buf, 0, numRead);
          out.write(buf,0,numRead);
        }
    
        entry.setCrc( crc.getValue());
    
        in.close(); 
    
      }
    
    }

Similar Threads

  1. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM
  2. Replies: 7
    Last Post: 05-23-2008, 03:46 AM
  3. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM
  4. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 04:22 PM
  5. Help with File in java
    By zoe in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 11:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •