Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-03-2008, 06:14 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default Creating ZIP files
The example below shows how to compress files. The presented Zip class is very for compressing.

Code:
import java.io.*;
import java.util.zip.*;

class Zip {
  public static void main(String args[]) throws IOException {
    byte b[] = new byte[512];
    ZipOutputStream zout = new ZipOutputStream(System.out);
    for(int i = 0; i < args.length; i ++) {
      InputStream in = new FileInputStream(args[i]);
      ZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/'));
      zout.putNextEntry(e);
      int len=0;
      while((len=in.read(b)) != -1) {
        zout.write(b,0,len);
        }
      zout.closeEntry();
      print(e);
      }
    zout.close();
    }
    
  public static void print(ZipEntry e){
    PrintStream err = System.err;
    err.print("added " + e.getName());
    if (e.getMethod() == ZipEntry.DEFLATED) {
      long size = e.getSize();
      if (size > 0) {
        long csize = e.getCompressedSize();
        long ratio = ((size-csize)*100) / size;
        err.println(" (deflated " + ratio + "%)");
        }
      else {
        err.println(" (deflated 0%)");
        }
      }
    else {
      err.println(" (stored 0%)");
      }
    }
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating .Jar files Deathmonger New To Java 6 06-23-2008 12:59 AM
Creating JAR files - 2 JavaForums Java Blogs 0 04-14-2008 06:11 PM
Creating JAR files - 1 JavaForums Java Blogs 0 04-14-2008 02:10 PM
Creating JAR files JavaForums Java Blogs 0 12-24-2007 01:30 PM
Creating ZIP files JavaForums Java Blogs 0 12-21-2007 11:40 AM


All times are GMT +2. The time now is 09:50 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org