Search in sources :

Example 1 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry 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 ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry 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 3 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry 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 4 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry 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)

Example 5 with ZipArchiveEntry

use of org.apache.commons.compress.archivers.zip.ZipArchiveEntry 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) IOException(java.io.IOException)

Aggregations

ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)46 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)21 IOException (java.io.IOException)13 File (java.io.File)12 FileInputStream (java.io.FileInputStream)10 InputStream (java.io.InputStream)10 Path (java.nio.file.Path)10 Test (org.junit.Test)8 BufferedInputStream (java.io.BufferedInputStream)7 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)7 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)6 FileOutputStream (java.io.FileOutputStream)5 ArrayList (java.util.ArrayList)5 ZipInputStream (java.util.zip.ZipInputStream)5 ImageInfo (com.github.hmdev.info.ImageInfo)4 SectionInfo (com.github.hmdev.info.SectionInfo)4 BufferedWriter (java.io.BufferedWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileNotFoundException (java.io.FileNotFoundException)4