Search in sources :

Example 91 with ZipFile

use of java.util.zip.ZipFile in project tinker by Tencent.

the class FileOperation method getZipEntryMd5.

public static String getZipEntryMd5(File file, String entryName) {
    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(file);
        ZipEntry entry = zipFile.getEntry(entryName);
        if (entry == null) {
            return null;
        }
        return MD5.getMD5(zipFile.getInputStream(entry), 1024 * 100);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 92 with ZipFile

use of java.util.zip.ZipFile in project tinker by Tencent.

the class FileUtil method mergeZip.

/**
     * merge zip file
     *
     * @param zipOutputFullFilename
     * @param mergeZipFullFilenameList
     */
public static void mergeZip(String zipOutputFullFilename, List<String> mergeZipFullFilenameList) {
    FileUtil.createFile(zipOutputFullFilename);
    ZipOutputStream zipOutputStream = null;
    try {
        zipOutputStream = new ZipOutputStream(new FileOutputStream(zipOutputFullFilename));
        if (mergeZipFullFilenameList != null) {
            for (String zipFullFilename : mergeZipFullFilenameList) {
                if (isExist(zipFullFilename)) {
                    ZipFile zipFile = new ZipFile(zipFullFilename);
                    Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
                    while (enumeration.hasMoreElements()) {
                        ZipEntry zipEntry = enumeration.nextElement();
                        InputStream inputStream = zipFile.getInputStream(zipEntry);
                        addZipEntry(zipOutputStream, zipEntry, inputStream);
                    }
                    zipFile.close();
                }
            }
        }
    } catch (Exception e) {
        throw new FileUtilException(e);
    } finally {
        try {
            if (zipOutputStream != null) {
                zipOutputStream.close();
            }
        } catch (Exception e) {
            throw new FileUtilException(e);
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 93 with ZipFile

use of java.util.zip.ZipFile in project sonarqube by SonarSource.

the class ZipUtilsTest method zip_directory.

@Test
public void zip_directory() throws IOException {
    File foo = FileUtils.toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldZipDirectory/foo.txt"));
    File dir = foo.getParentFile();
    File zip = temp.newFile();
    ZipUtils.zipDir(dir, zip);
    assertThat(zip).exists().isFile();
    assertThat(zip.length()).isGreaterThan(1L);
    Iterator<? extends ZipEntry> zipEntries = Iterators.forEnumeration(new ZipFile(zip).entries());
    assertThat(zipEntries).hasSize(4);
    File unzipDir = temp.newFolder();
    ZipUtils.unzip(zip, unzipDir);
    assertThat(new File(unzipDir, "bar.txt")).exists().isFile();
    assertThat(new File(unzipDir, "foo.txt")).exists().isFile();
    assertThat(new File(unzipDir, "dir1/hello.properties")).exists().isFile();
}
Also used : ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 94 with ZipFile

use of java.util.zip.ZipFile in project atlas by alibaba.

the class BundleArchiveRevision method installSoLib.

/**
     * 安装so库
     * @throws IOException
     */
public void installSoLib(File bundle) throws IOException {
    ZipFile zip = null;
    try {
        zip = new ZipFile(bundle);
        String extractTag = "lib/armeabi";
        if (Build.CPU_ABI.contains("x86")) {
            if (zip.getEntry("lib/x86/") != null) {
                extractTag = "lib/x86";
            }
        }
        for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
            ZipEntry zipEntry = (ZipEntry) entries.nextElement();
            String entryName = zipEntry.getName();
            if (entryName.equalsIgnoreCase("../")) {
                continue;
            }
            if (entryName.indexOf(extractTag) != -1) {
                extractEntry(zip, zipEntry);
            }
        }
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        if (null != zip) {
            zip.close();
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) MalformedURLException(java.net.MalformedURLException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 95 with ZipFile

use of java.util.zip.ZipFile in project atlas by alibaba.

the class DexReleaser method releaseDexes.

public static boolean releaseDexes(File bundleFile, File reversionDir) throws IOException {
    ZipFile zipFile = new ZipFile(bundleFile);
    boolean hasDexFile = hasDexFile(zipFile);
    if (!hasDexFile) {
        return true;
    }
    if (!isArt()) {
        Enumeration entryEnumeration = zipFile.entries();
        while (entryEnumeration.hasMoreElements()) {
            ZipEntry zipEntry = (ZipEntry) entryEnumeration.nextElement();
            if (zipEntry.getName().endsWith(DEX_SUFFIX)) {
                File dexFile = new File(reversionDir, zipEntry.getName());
                FileOutputStream fileOutputStream = new FileOutputStream(dexFile);
                copy(zipFile.getInputStream(zipEntry), fileOutputStream);
                fileOutputStream.close();
            }
        }
        return true;
    } else {
        File mergedFile = preProcessPatch(zipFile, bundleFile);
        if (mergedFile != null && mergedFile.exists()) {
            bundleFile.delete();
            boolean success = mergedFile.renameTo(bundleFile);
            if (!success || !isNewBundleFileValid(bundleFile)) {
                return false;
            }
        } else {
            return false;
        }
        try {
            zipFile.close();
        } catch (Throwable e) {
        }
    }
    return true;
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ZipFile(java.util.zip.ZipFile)

Aggregations

ZipFile (java.util.zip.ZipFile)637 ZipEntry (java.util.zip.ZipEntry)454 File (java.io.File)287 IOException (java.io.IOException)214 InputStream (java.io.InputStream)147 FileOutputStream (java.io.FileOutputStream)108 ZipOutputStream (java.util.zip.ZipOutputStream)92 Test (org.junit.Test)89 FileInputStream (java.io.FileInputStream)68 Enumeration (java.util.Enumeration)47 ArrayList (java.util.ArrayList)46 BufferedInputStream (java.io.BufferedInputStream)44 BufferedOutputStream (java.io.BufferedOutputStream)39 ZipInputStream (java.util.zip.ZipInputStream)35 ZipException (java.util.zip.ZipException)34 OutputStream (java.io.OutputStream)31 ClassReader (org.objectweb.asm.ClassReader)29 FileNotFoundException (java.io.FileNotFoundException)26 JarFile (java.util.jar.JarFile)26 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24