Search in sources :

Example 31 with ZipFile

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

the class ProjectFilesystemTest method testCreateZipPreservesExecutablePermissions.

@Test
public void testCreateZipPreservesExecutablePermissions() throws IOException {
    // Create a empty executable file.
    Path exe = tmp.newFile("test.exe");
    MoreFiles.makeExecutable(exe);
    // Archive it into a zipfile using `ProjectFileSystem.createZip`.
    Path zipFile = tmp.getRoot().resolve("test.zip");
    filesystem.createZip(ImmutableList.of(exe), zipFile);
    // permissions.
    try (ZipFile zip = new ZipFile(zipFile.toFile())) {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        assertTrue(entries.hasMoreElements());
        ZipArchiveEntry entry = entries.nextElement();
        Set<PosixFilePermission> permissions = MorePosixFilePermissions.fromMode(entry.getExternalAttributes() >> 16);
        assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE));
        assertFalse(entries.hasMoreElements());
    }
}
Also used : Path(java.nio.file.Path) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Example 32 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 33 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 34 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 35 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)

Aggregations

ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)44 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)25 File (java.io.File)23 IOException (java.io.IOException)23 FileNotFoundException (java.io.FileNotFoundException)12 InputStream (java.io.InputStream)12 BufferedInputStream (java.io.BufferedInputStream)10 FileInputStream (java.io.FileInputStream)9 ZipEntry (java.util.zip.ZipEntry)8 Path (java.nio.file.Path)7 DataAccessException (org.vcell.util.DataAccessException)7 ZipInputStream (java.util.zip.ZipInputStream)6 DataInputStream (java.io.DataInputStream)5 FileOutputStream (java.io.FileOutputStream)5 ArrayList (java.util.ArrayList)5 ZipOutputStream (java.util.zip.ZipOutputStream)5 OutputStream (java.io.OutputStream)4 Test (org.junit.Test)4 MathException (cbit.vcell.math.MathException)2 VariableType (cbit.vcell.math.VariableType)2