Coverage details for base.util.ZipUtil

LineHitsSource
1 /*******************************************************************************
2  *InternetCafe is a software solution that helps the management of Cybercafes
3  * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4  * Copyright (C) 2006 Guido Angelo Ingenito
5  
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *******************************************************************************/
20 package base.util;
21  
22 import java.io.BufferedInputStream;
23 import java.io.BufferedOutputStream;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Enumeration;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipFile;
32 import java.util.zip.ZipOutputStream;
33  
34 import org.apache.log4j.Logger;
35  
36 /**
37  * This class provides some basic methods to compress/uncompress files and
38  * folders in zip compression format.
39  *
40  * @author Guido Angelo Ingenito
41  */
423public class ZipUtil {
43  
443    private static final transient Logger logger = Logger
45             .getLogger(ZipUtil.class.getName());
46  
47     /**
48      * This method is a wrapper for the method the zipDirectory(File,
49      * ZipOutputStream). It provides a more easy interface to be used when must
50      * be zipped a file or a folder.
51      *
52      * @param inputFile
53      * The input file/folder to be zipped.
543     * @param outputFile
553     * The zipped outputted file.
563     * @throws IOException
573     * If something wrong happpens.
583     */
59     public static void zipDirectory(File inputFile, File outputFile)
60             throws IOException {
610        logger.debug("zipDirectory(in:" + inputFile.toString() + ", out:"
62                 + outputFile.toString() + ")");
630        ZipOutputStream zipOutputStream = new ZipOutputStream(
64                 new FileOutputStream(outputFile));
650        zipDirectory(inputFile, zipOutputStream);
660        zipOutputStream.close();
673    }
683 
693    /**
70303     * This method provides a simple zip compression for a file or an entire
71300     * folder.
72300     *
73      * @param inputFile
74      * The input file/folder whose content must be zipped.
750     * @param zipOutputStream
76      * The target output stream that points to the location where the
770     * zip file must be placed.
78      * @throws IOException
79      * If something wrong happens.
80      */
81300    public static void zipDirectory(File inputFile,
82             ZipOutputStream zipOutputStream) throws IOException {
83300        String[] dirList = inputFile.list();
840        byte[] readBuffer = new byte[1024];
850        int bytesIn = 0;
86300        for (int i = 0; i < dirList.length; i++) {
870            File file = new File(inputFile, dirList[i]);
882100            if (file.isDirectory()) {
891500                // if the File object is a directory, call this
900                // function again to add its content recursively
910                zipDirectory(file, zipOutputStream);
92300                // loop again
930                continue;
943            }
95             // if we reached here, the File object file was not a directory
96             // create a FileInputStream on top of file
970            FileInputStream fileInputStream = new FileInputStream(file);
980            // create a new zip entry
990            ZipEntry zipEntry = new ZipEntry(file.getPath());
1000 
1010            // place the zip entry in the ZipOutputStream object
1020            zipOutputStream.putNextEntry(zipEntry);
103             // now write the content of the file to the ZipOutputStream
1040            while ((bytesIn = fileInputStream.read(readBuffer)) != -1) {
1050                zipOutputStream.write(readBuffer, 0, bytesIn);
1060            }
1070            // close the Stream
1080            fileInputStream.close();
109         }
1100    }
1110 
112     public static void zipAllFile(File[] inputFile, File outputFile)
1130            throws IOException {
1140        logger.debug("zipAllFile(in:" + inputFile.length + ", out:"
115                 + outputFile.toString() + ")");
1160        ZipOutputStream zipOutputStream = new ZipOutputStream(
1170                new FileOutputStream(outputFile));
1180        zipAllFile(inputFile, zipOutputStream);
1190        zipOutputStream.close();
1200    }
121  
1220    public static void zipAllFile(File[] inputFile,
123             ZipOutputStream zipOutputStream) throws IOException {
1240 
1250        byte[] readBuffer = new byte[1024];
1260        int bytesIn = 0;
1270        for (int i = 0; i < inputFile.length; i++) {
1280            if (inputFile[i].isDirectory()) {
129                 // if the File object is a directory, call this
1300                // function again to add its content recursively
1310                zipDirectory(inputFile[i], zipOutputStream);
132                 // loop again
1330                continue;
134             }
135             // if we reached here, the File object file was not a directory
136             // create a FileInputStream on top of file
1370            FileInputStream fileInputStream = new FileInputStream(inputFile[i]);
138             // create a new zip entry
1390            ZipEntry zipEntry = new ZipEntry(inputFile[i].getPath());
1400 
1410            // place the zip entry in the ZipOutputStream object
1420            zipOutputStream.putNextEntry(zipEntry);
1430            // now write the content of the file to the ZipOutputStream
1440            while ((bytesIn = fileInputStream.read(readBuffer)) != -1) {
1450                zipOutputStream.write(readBuffer, 0, bytesIn);
1460            }
147             // close the Stream
1480            fileInputStream.close();
149         }
1500    }
151  
152     /**
153      * This method is a wrapper for the method the zipFile(File,
1540     * ZipOutputStream). It provides a more easy interface to be used when must
1550     * be zipped a file (not a folder).
1560     *
157      * @param inputFile
158      * The input file (not a folder) to be zipped.
1590     * @param outputFile
1600     * The zipped outputted file.
1610     * @throws IOException
162      * If something wrong happpens.
1630     */
164     public static void zipFile(File inputFile, File outputFile)
1650            throws IOException {
1660        logger.debug("zipFile(in:" + inputFile.toString() + ", out:"
167                 + outputFile.toString() + ")");
1680        ZipOutputStream zipOutputStream = new ZipOutputStream(
169                 new FileOutputStream(outputFile));
1700        zipFile(inputFile, zipOutputStream);
1710        zipOutputStream.close();
1720    }
173  
174     /**
175      * This method provides a simple zip compression for a file (not a folder).
1760     *
1770     * @param inputFile
1780     * The file that must be zippede.
1790     * @param zipOutputStream
1800     * The target output stream that points to the location where the
1810     * zip file must be placed.
182      * @throws IOException
1830     * If something wrong happens.
1840     */
1850    public static void zipFile(File inputFile, ZipOutputStream zipOutputStream)
1860            throws IOException {
1870        // Create a buffer for reading the file
1880        byte[] buf = new byte[1024];
1890        zipOutputStream.putNextEntry(new ZipEntry(inputFile.getName()));
1900        FileInputStream fileInputStream = new FileInputStream(inputFile);
1910        // Transfer bytes from the file to the ZIP file
1920        int length;
1930        while ((length = fileInputStream.read(buf)) > 0) {
1940            zipOutputStream.write(buf, 0, length);
1950        }
1960        // close the input stream
1970        fileInputStream.close();
1980        // Complete the entry
1990        zipOutputStream.closeEntry();
2000    }
2010 
2020    /**
2030     * This method provides a simple unzip facility for a composite zip file (a
204      * zip file that contains files and folders).
205      *
206      * @param inputFile
207      * The zip file that must be extracted.
208      * @param outputDirectory
209      * The destination folder where the content of the input zipped
210      * file must be placed.
211      * @throws IOException
212      * If something wrong happens.
213      */
214     public static void unZipFile(File inputFile, File outputDirectory)
215             throws IOException {
2160        logger.debug("unZipFile(in:" + inputFile.toString() + ", out:"
217                 + outputDirectory.toString() + ")");
2180        ZipFile zipFile = new ZipFile(inputFile);
2190        Enumeration zipEntries = zipFile.entries();
2200        while (zipEntries.hasMoreElements()) {
2210            ZipEntry zipEntry = (ZipEntry) zipEntries.nextElement();
2220            logger.debug("Unpacking: " + zipEntry.getName());
223  
2240            File file = new File(outputDirectory, zipEntry.getName());
2250            if (zipEntry.isDirectory()) {
2260                file.mkdirs();
2270            } else {
2280                InputStream inputStream = zipFile.getInputStream(zipEntry);
2290                BufferedInputStream bis = new BufferedInputStream(inputStream);
2300                File dir = new File(file.getParent());
2310                dir.mkdirs();
2320                FileOutputStream fos = new FileOutputStream(file);
2330                BufferedOutputStream bos = new BufferedOutputStream(fos);
234  
235                 int readByte;
2360                while ((readByte = bis.read()) != -1) {
2370                    bos.write((byte) readByte);
2380                }
2390                bos.close();
2400                fos.close();
241             }
2420            logger.debug(zipEntry.getName() + " : Unpacked.");
2430        }
2440    }
245 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.