Project

General

Profile

Download (2.7 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2015 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.server;
10

    
11
import java.io.BufferedOutputStream;
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.util.zip.ZipEntry;
17
import java.util.zip.ZipInputStream;
18

    
19
import org.apache.commons.io.FileUtils;
20
import org.apache.log4j.Logger;
21

    
22
/**
23
 * This utility extracts files and directories of a standard zip file to
24
 * a destination directory.
25
 * @author www.codejava.net
26
 */
27
public class UnzipUtility {
28

    
29
    private static final Logger logger = Logger.getLogger(UnzipUtility.class);
30

    
31
    /**
32
     * Size of the buffer to read/write data
33
     */
34
    private static final int BUFFER_SIZE = 4096;
35
    /**
36
     * Extracts a zip file specified by the zipFilePath to a directory specified by
37
     * destDirectory (will be created if does not exists)
38
     * @param zipFilePath
39
     * @param destDirectory
40
     * @throws IOException
41
     */
42
    public void unzip(File zipFile, File destDir) throws IOException {
43
        if (!destDir.exists()) {
44
            destDir.mkdir();
45
        }
46
        String destPath = destDir.getAbsolutePath();
47
        logger.info("unpacking " + zipFile + " into " + destPath);
48
        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFile));
49
        ZipEntry entry = zipIn.getNextEntry();
50
        // iterates over entries in the zip file
51
        while (entry != null) {
52
            String filePath = destPath + File.separator + entry.getName();
53
            if (!entry.isDirectory()) {
54
                // if the entry is a file, extracts it
55
                extractFile(zipIn, filePath);
56
            } else {
57
                // if the entry is a directory, make the directory
58
                File dir = new File(filePath);
59
                dir.mkdir();
60
            }
61
            zipIn.closeEntry();
62
            entry = zipIn.getNextEntry();
63
        }
64
        zipIn.close();
65
    }
66
    /**
67
     * Extracts a zip entry (file entry)
68
     * @param zipIn
69
     * @param filePath
70
     * @throws IOException
71
     */
72
    private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
73

    
74
        File file = new File(filePath);
75
        FileUtils.forceMkdir(file.getParentFile());
76

    
77
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
78
        byte[] bytesIn = new byte[BUFFER_SIZE];
79
        int read = 0;
80
        while ((read = zipIn.read(bytesIn)) != -1) {
81
            bos.write(bytesIn, 0, read);
82
        }
83
        bos.close();
84
    }
85
}
(6-6/6)