Compress files using the Java ZIP API : File Commands « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » File Input Output » File CommandsScreenshots 
Compress files using the Java ZIP API

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;

public class Compress {
  public static void gzipFile(String from, String tothrows IOException {
    FileInputStream in = new FileInputStream(from);
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1)
      out.write(buffer, 0, bytesRead);
    in.close();
    out.close();
  }

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

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

    for (int i = 0; i < entries.length; i++) {
      File f = new File(d, entries[i]);
      if (f.isDirectory())
        continue;//Ignore directory
      FileInputStream in = new FileInputStream(f)// Stream to read file
      ZipEntry entry = new ZipEntry(f.getPath())// Make a ZipEntry
      out.putNextEntry(entry)// Store entry
      while ((bytesRead = in.read(buffer)) != -1)
        out.write(buffer, 0, bytesRead);
      in.close()
    }
    out.close();
  }

  public static void main(String args[]) throws IOException {
    String from ".";
    File f = new File(from);
    boolean directory = f.isDirectory()// Is it a file or directory?

    Compress.zipDirectory(from, from ".zip");
    Compress.gzipFile(from, from ".gz");
  }
}

           
       
Related examples in the same category
1. File Copy in Java
2. Counts words in a file, outputs results in sorted form
3. Copying a file using channels and buffers
4. Copy files using Java IO APICopy files using Java IO API
5. Mimic the Unix Grep command
6. File concatenation
7. Delete file using Java IO API
8. Undent - remove leading spaces
9. TeePrintStream tees all PrintStream operations into a file, rather like the UNIX tee(1) command
10. Delete a file from within Java, with error handling
11. DirTree - directory lister, like UNIX ls or DOS and VMS dirDirTree - directory lister, like UNIX ls or DOS and VMS dir
12. Program to empty a directory
13. Report on a file's status in Java
14. Simple directory lister
15. Readonly Files
16. List root directoryList root directory
17. Rename a file in Java
18. FNFilter - directory lister using FilenameFilter
19. mkdir examples
20. Program to remove files matching a name in a directory
21. Ls directory lister modified to use FilenameFilterLs directory lister modified to use FilenameFilter
22. Dir Stack Dir Stack
23. Word Count
24. Diff: text file difference utility.Diff: text file difference utility.
25. Count chars in a File
w_ww___._ja_v_a___2__s__.com__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.