Project

General

Profile

Download (2.74 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.logging.log4j.LogManager;
21
import org.apache.logging.log4j.Logger;
22

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

    
30
    private static final Logger logger = LogManager.getLogger();
31

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

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

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