Makes a zip file named xmlFileName from xmlURL at path : Zip Tar File « 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 » Zip Tar FileScreenshots 
Makes a zip file named xmlFileName from xmlURL at path
     
/**
 * Copyright (c) 2003 Daffodil Software Ltd all rights reserved.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * There are special exceptions to the terms and conditions of the GPL
 * as it is applied to this software. See the GNU General Public License for more details.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */


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


/**
 * This class is used to make zip file of the XML file,BLOB.lob and CLOB.lob
 * so that compacted files can be transferred over the network. Besides it, this
 * class contains method for unzipping it.
 */

public class ZipHandler
{

    /**
     * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL>
     @param zipURL
     @param xmlURL
     @param xmlFileName
     */


    public static void makeZip(String zipURL, String xmlURL, String xmlFileNamethrows
       IOException
   {
       FileOutputStream fos = new FileOutputStream(new File(zipURL));
       ZipOutputStream zos = new ZipOutputStream(fos);
//         zos.setLevel();
       FileInputStream fis = new FileInputStream(xmlURL);
       zos.putNextEntry(new ZipEntry(xmlFileName+".xml"));
       writeInOutputStream(fis,zos);
       String bpath = "c:\\";
       FileInputStream fisBLOB = createInputStream(bpath);
       zos.putNextEntry(new ZipEntry("blob.lob"));
       writeInOutputStream(fisBLOB, zos);

       zos.closeEntry();
       String cpath = "c:\\";
       FileInputStream fisCLOB = createInputStream(cpath);
       zos.putNextEntry(new ZipEntry("clob.lob"));
       writeInOutputStream(fisCLOB, zos);
       zos.closeEntry();
       fis.close();
       fisCLOB.close();
       fisBLOB.close();
       zos.close();
       fos.close();

   }


    /**
     * unzipps a zip file placed at <zipURL> to path <xmlURL>
     @param zipURL
     @param xmlURL
     */
    public static void unZip(String zipURL, String xmlURLthrows IOException
    {
        FileOutputStream fosBLOB = new FileOutputStream(new File("c:\\"));
        FileOutputStream fosCLOB = new FileOutputStream(new File("c:\\"));
        FileInputStream fis = new FileInputStream(new File(zipURL));
        ZipInputStream zis = new ZipInputStream(fis);
        FileOutputStream fos = new FileOutputStream(xmlURL);
        ZipEntry ze = zis.getNextEntry();
        ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
        ze = zis.getNextEntry();
        ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
        ze = zis.getNextEntry();
        ExtractZip(zis, fos, fosBLOB, fosCLOB, ze);
        fos.flush();
        fis.close();
        fos.close();
        fosCLOB.close();
        fosBLOB.close();
        zis.close();
        }

    private static void ExtractZip(ZipInputStream zis, FileOutputStream fos,
                                   FileOutputStream fosBLOB,
                                   FileOutputStream fosCLOB, ZipEntry zethrows
        IOException {
        if (ze.getName().equalsIgnoreCase("blob.lob"))
          writeInOutputStream(zis, fosBLOB);
        else if (ze.getName().equalsIgnoreCase("clob.lob"))
        writeInOutputStream(zis, fosCLOB);
        else
          writeInOutputStream(zis, fos);
        }

    private static void writeInOutputStream(InputStream is,
                                            OutputStream osthrows
        IOException {
            byte[] buf = new byte[1024];
            int len = 0;
      while ( (len = is.read(buf)) 0)
            {
          os.write(buf, 0, len);
            }
        }

    /**
     * makes a zip file named <xmlFileName> from <xmlURL> at path <zipURL>
     @param zipURL
     @param xmlURL
     @param xmlFileName
     */

    public static void makeStructZip(String zipURL, String xmlURL, String xmlFileNamethrows
        IOException
        {
        FileOutputStream fos = new FileOutputStream(new File(zipURL));
        ZipOutputStream zos = new ZipOutputStream(fos);
//         zos.setLevel();
        FileInputStream fis = new FileInputStream(xmlURL);
        zos.putNextEntry(new ZipEntry(xmlFileName+".xml"));
        writeInOutputStream(fis,zos);
        zos.closeEntry();
        fis.close();
        zos.close();
            }

    /**
     * unzipps a zip file placed at <zipURL> to path <xmlURL>
     @param zipURL
     @param xmlURL
     */
    public static void unStructZip(String zipURL, String xmlURLthrows IOException
        {
        FileInputStream fis = new FileInputStream(new File(zipURL));
        ZipInputStream zis = new ZipInputStream(fis);
        FileOutputStream fos = new FileOutputStream(xmlURL);
        ZipEntry ze = zis.getNextEntry();
        writeInOutputStream(zis,fos);
        fos.flush();
        fis.close();
        fos.close();
        zis.close();
    }

    private static FileInputStream createInputStream(String bpaththrows IOException{
      FileInputStream fis = null;
      try
      {
          fis = new FileInputStream(bpath);
      }
      catch (FileNotFoundException ex)
      {
          FileOutputStream temp = new FileOutputStream(bpath);
          fis = new FileInputStream(bpath);
      }
      return fis;
    }
}

   
    
    
    
    
  
Related examples in the same category
1. Extract contents of a zip file
2. List the contents of a zip file
3. Read entries in a zip / compressed file
4. Decompress a zip file using ZipInputStream
5. Decompress a zip file using ZipFile
6. Create checksum for a zip file
7. Read a zip file checksum value
8. Create a zip file with java.util.zip package
9. Extract file/files from a zip file
10. Read files within a zip file
11. Retrieve a compressed file from a ZIP file
12. Retrieve the contents of a ZIP file
13. Making a zip file of directory including its subdirectories recursively
14. Displaying contents of a compressed zip file
15. Compress a Byte Array
16. Decompress a Byte Array
17. Read zip file
18. Write Zip file
19. The java.util.zip package can be used to create a checksum.
20. Read the content of a zip file ZipFile
21. List the entries of a zip file
22. Compressing Streams: Zipper, Java example
23. Compressing Streams: Parity Checksum
24. Compressing Streams: File Summer
25. Create a simple ZIP File: not retain any directory path information about the files.
26. Decompress a ZIP file.
27. Decompressing a Byte Array
28. Zip unzip byte array
29. Creating a ZIP File
30. Listing the Contents of a ZIP File
31. Retrieving a Compressed File from a ZIP File
32. Calculating the Checksum of a Byte Array (Compute Adler-32 checksum)
33. Compute CRC-32 checksum
34. Calculating the Checksum of a File
35. Compress string(byte array) by Deflater
36. Use Java code to zip a folder
37. Uses Zip compression to compress any number of files given on the command line
38. Load zip file and scan zip file
39. Reading the Contents of a ZIP File
40. UnZip -- print or unzip a JAR or PKZIP file using java.util.zip
41. Tape Archive Lister: Tar file
42. Compressing a Byte Array
43. Tar file stream
44. Tar file and untar file
45. bzip source code
46. Unpack an archive from a URL
47. Compare two zip files
48. Determine whether a file is a ZIP File.
49. Zip up a directory
50. Check sum for a path
51. Check sum for an InputStream
52. Extract zip file to destination folder
53. Return the first directory of this archive. This is needed to determine the plugin directory
54. Unzipps a zip file placed at zipURL to path
55. A single checksum calculation for multiple files
56. Put file To Zip File
57. Provides both writing and reading from a file which is transparently compressed in Zip
58. TarInputStream reads a UNIX tar archive as an InputStream
59. TarOutputStream writes a UNIX tar archive as an OutputStream
60. Package files utility
61. Zip Compare
62. Unpack a segment from a zip
63. Unpack a zip file
64. Unzip file to a directory
65. Zip a list of file into one zip file.
66. Validate that an archive contains a named entry
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.