Search in sources :

Example 21 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.

the class ZipUtils method isZipFile.

/**
     * <p>
     * isZipFile.
     * </p>
     *
     * @param zipFile a {@link java.io.File} object.
     * @return a boolean.
     */
public static boolean isZipFile(File zipFile) {
    try {
        ZipFile zf = new ZipFile(zipFile);
        boolean isZip = zf.getEntries().hasMoreElements();
        zf.close();
        return isZip;
    } catch (IOException e) {
        return false;
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile)

Example 22 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.

the class ZipUtils method isFolderExist.

/**
     * 判断在指定的zip目录下,指定的文件夹是否存在
     *
     * @param zipFile
     * @param pathName
     * @return
     */
public static boolean isFolderExist(File zipFile, String pathName) {
    ZipFile file = null;
    try {
        file = new ZipFile(zipFile);
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        while (en.hasMoreElements()) {
            ZipArchiveEntry entry = en.nextElement();
            String name = entry.getName();
            if (name.startsWith(pathName)) {
                return true;
            }
        }
        return false;
    } catch (IOException e) {
    } finally {
        if (null != file) {
            try {
                file.close();
            } catch (IOException e) {
            }
        }
    }
    return false;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Example 23 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.

the class ZipUtils method extractZipFileToFolder.

/**
     * 压zip文件中的某个文件到指定地方
     *
     * @param zipFile
     * @param path
     * @param destFolder
     * @throws java.io.IOException
     */
public static File extractZipFileToFolder(File zipFile, String path, File destFolder) {
    ZipFile zip;
    File destFile = null;
    try {
        zip = new ZipFile(zipFile);
        ZipArchiveEntry zipArchiveEntry = zip.getEntry(path);
        if (null != zipArchiveEntry) {
            String name = zipArchiveEntry.getName();
            name = FilenameUtils.getName(name);
            destFile = new File(destFolder, name);
            FileMkUtils.mkdirs(destFolder);
            destFile.createNewFile();
            InputStream is = zip.getInputStream(zipArchiveEntry);
            FileOutputStream fos = new FileOutputStream(destFile);
            int length = 0;
            byte[] b = new byte[1024];
            while ((length = is.read(b, 0, 1024)) != -1) {
                fos.write(b, 0, length);
            }
            is.close();
            fos.close();
        }
        if (null != zip)
            ZipFile.closeQuietly(zip);
    } catch (IOException e) {
    }
    return destFile;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipInputStream(java.util.zip.ZipInputStream) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile)

Example 24 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project atlas by alibaba.

the class ZipUtils method unzip.

/**
     * <p>
     * unzip.
     * </p>
     *
     * @param zipFile     a {@link java.io.File} object.
     * @param destination a {@link String} object.
     * @param encoding    a {@link String} object.
     * @return a {@link java.util.List} object.
     */
public static List<String> unzip(final File zipFile, final String destination, String encoding) {
    List<String> fileNames = new ArrayList<String>();
    String dest = destination;
    if (!destination.endsWith(File.separator)) {
        dest = destination + File.separator;
    }
    ZipFile file;
    try {
        file = null;
        if (null == encoding)
            file = new ZipFile(zipFile);
        else
            file = new ZipFile(zipFile, encoding);
        Enumeration<ZipArchiveEntry> en = file.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            File f = new File(dest, ze.getName());
            if (ze.isDirectory()) {
                f.mkdirs();
                continue;
            } else {
                f.getParentFile().mkdirs();
                InputStream is = file.getInputStream(ze);
                OutputStream os = new FileOutputStream(f);
                IOUtils.copy(is, os);
                is.close();
                os.close();
                fileNames.add(f.getAbsolutePath());
            }
        }
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileNames;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipInputStream(java.util.zip.ZipInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ArrayList(java.util.ArrayList) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile)

Example 25 with ZipFile

use of org.apache.commons.compress.archivers.zip.ZipFile in project tika by apache.

the class CXFTestBase method readZipArchive.

protected Map<String, String> readZipArchive(InputStream inputStream) throws IOException {
    Map<String, String> data = new HashMap<String, String>();
    Path tempFile = writeTemporaryArchiveFile(inputStream, "zip");
    ZipFile zip = new ZipFile(tempFile.toFile());
    Enumeration<ZipArchiveEntry> entries = zip.getEntries();
    while (entries.hasMoreElements()) {
        ZipArchiveEntry entry = entries.nextElement();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOUtils.copy(zip.getInputStream(entry), bos);
        data.put(entry.getName(), DigestUtils.md5Hex(bos.toByteArray()));
    }
    zip.close();
    Files.delete(tempFile);
    return data;
}
Also used : Path(java.nio.file.Path) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) HashMap(java.util.HashMap) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)27 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)21 IOException (java.io.IOException)10 File (java.io.File)7 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 ZipInputStream (java.util.zip.ZipInputStream)6 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 BufferedInputStream (java.io.BufferedInputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 OutputStream (java.io.OutputStream)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)2 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)1