Search in sources :

Example 1 with ZipFile

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

the class ZipUtils method unzip.

public static List<String> unzip(final File zipFile, final String destination, String encoding, Map<String, ZipEntry> zipEntryMethodMap, boolean isRelativePath) {
    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());
                if (zipEntryMethodMap != null && ze.getMethod() == STORED) {
                    zipEntryMethodMap.put(isRelativePath ? ze.getName() : f.getAbsolutePath(), ze);
                }
            }
        }
        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 2 with ZipFile

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

the class ZipUtils method removeZipEntry.

public static boolean removeZipEntry(File file, Pattern pattern, File targetFile) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[1024];
    java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(file);
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(targetFile)));
    BufferedOutputStream bo = new BufferedOutputStream(out);
    InputStream inputStream;
    Enumeration enumeration = zipFile.entries();
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
        String name = zipEntry.getName();
        if (pattern.matcher(name).find()) {
            continue;
        }
        out.putNextEntry(zipEntry);
        inputStream = zipFile.getInputStream(zipEntry);
        write(inputStream, out, buffer);
        bo.flush();
    }
    closeQuitely(zipFile);
    closeQuitely(out);
    closeQuitely(bo);
    return true;
}
Also used : Enumeration(java.util.Enumeration) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream)

Example 3 with ZipFile

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

the class ZipUtils method listZipEntries.

public static List<String> listZipEntries(File zipFile) {
    List<String> list = new ArrayList<String>();
    ZipFile zip;
    try {
        zip = new ZipFile(zipFile);
        Enumeration<ZipArchiveEntry> en = zip.getEntries();
        ZipArchiveEntry ze = null;
        while (en.hasMoreElements()) {
            ze = en.nextElement();
            String name = ze.getName();
            name = StringUtils.replace(name, "/", ".");
            if (name.endsWith(".class")) {
                list.add(name);
            }
        }
        if (null != zip)
            ZipFile.closeQuietly(zip);
    } catch (IOException e) {
    }
    return list;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ArrayList(java.util.ArrayList) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)

Example 4 with ZipFile

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

the class PatchUtils method isBundleFile.

/**
     * 判断当前so文件是否为bundle
     *
     * @param soFile
     * @return
     */
public static boolean isBundleFile(File soFile) {
    ZipFile zf = null;
    try {
        zf = new ZipFile(soFile);
        Enumeration<ZipArchiveEntry> en = zf.getEntries();
        if (en.hasMoreElements()) {
            for (String name : BUNDLE_FILES) {
                ZipArchiveEntry zipArchiveEntry = zf.getEntry(name);
                if (null == zipArchiveEntry) {
                    return false;
                }
            }
            return true;
        }
        return false;
    } catch (IOException e) {
        return false;
    } finally {
        if (null != zf) {
            try {
                zf.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException)

Example 5 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 File} object.
     * @param destination a {@link String} object.
     * @param encoding    a {@link String} object.
     * @return a {@link 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) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return fileNames;
}
Also used : ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

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