gzipping files and zipping directories : GZIP « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » GZIPScreenshots 
gzipping files and zipping directories
  
 
/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */

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

/**
 * This class defines two static methods for gzipping files and zipping
 * directories. It also defines a demonstration program as a nested class.
 */
public class Compress {
  /** Gzip the contents of the from file and save in the to file. */
  public static void gzipFile(String from, String tothrows IOException {
    // Create stream to read from the from file
    FileInputStream in = new FileInputStream(from);
    // Create stream to compress data and write it to the to file.
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    // Copy bytes from one stream to the other
    byte[] buffer = new byte[4096];
    int bytes_read;
    while ((bytes_read = in.read(buffer)) != -1)
      out.write(buffer, 0, bytes_read);
    // And close the streams
    in.close();
    out.close();
  }

  /** Zip the contents of the directory, and save it in the zipfile */
  public static void zipDirectory(String dir, String zipfilethrows IOException,
      IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
      throw new IllegalArgumentException("Compress: not a directory:  " + dir);
    String[] entries = d.list();
    byte[] buffer = new byte[4096]// Create a buffer for copying
    int bytes_read;

    // Create a stream to compress data and write it to the zipfile
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

    // Loop through all entries in the directory
    for (int i = 0; i < entries.length; i++) {
      File f = new File(d, entries[i]);
      if (f.isDirectory())
        continue// Don't zip sub-directories
      FileInputStream in = new FileInputStream(f)// Stream to read file
      ZipEntry entry = new ZipEntry(f.getPath())// Make a ZipEntry
      out.putNextEntry(entry)// Store entry
      while ((bytes_read = in.read(buffer)) != -1)
        // Copy bytes
        out.write(buffer, 0, bytes_read);
      in.close()// Close input stream
    }
    // When we're done with the whole loop, close the output stream
    out.close();
  }

  /**
   * This nested class is a test program that demonstrates the use of the static
   * methods defined above.
   */
  public static class Test {
    /**
     * Compress a specified file or directory. If no destination name is
     * specified, append .gz to a file name or .zip to a directory name
     */
    public static void main(String args[]) throws IOException {
      if ((args.length != 1&& (args.length != 2)) { // check arguments
        System.err.println("Usage: java Compress$Test <from> [<to>]");
        System.exit(0);
      }
      String from = args[0], to;
      File f = new File(from);
      boolean directory = f.isDirectory()// Is it a file or directory?
      if (args.length == 2)
        to = args[1];
      else // If destination not specified
        if (directory)
          to = from ".zip"// use a .zip suffix
        else
          to = from ".gz"// or a .gz suffix
      }

      if ((new File(to)).exists()) { // Make sure not to overwrite
        System.err.println("Compress: won't overwrite existing file: " + to);
        System.exit(0);
      }

      // Finally, call one of the methods defined above to do the work.
      if (directory)
        Compress.zipDirectory(from, to);
      else
        Compress.gzipFile(from, to);
    }
  }
}

   
    
  
Related examples in the same category
1. GZip with GZIPOutputStream
2. Ungzip with GZIPInputStream
3. Uncompress a file in the GZIP format
4. Compressing a File in the GZIP Format
5. Uncompressing a File in the GZIP Format
6. Compress a file in the GZIP format
7. Compress Java objects
8. Decompress Java objects
9. Compressed socket
10. Compress a list of files passed in from command line
11. GZIP compress
12. Compressing a File
13. Read some data from a gzip file
14. A collection of utility methods for working on GZIPed data
15. Reads GZIP, Zip, and Jar files
16. GZIP Filter, response stream and Response Wrapper
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.