Search in sources :

Example 11 with ZipInputStream

use of java.util.zip.ZipInputStream in project buck by facebook.

the class AaptPackageResourcesIntegrationTest method testAaptPackageIsScrubbed.

@Test
public void testAaptPackageIsScrubbed() throws IOException {
    AssumeAndroidPlatform.assumeSdkIsAvailable();
    workspace.runBuckBuild(MAIN_BUILD_TARGET).assertSuccess();
    Path aaptOutput = workspace.getPath(BuildTargets.getGenPath(filesystem, BuildTargetFactory.newInstance(MAIN_BUILD_TARGET).withFlavors(AndroidBinaryGraphEnhancer.AAPT_PACKAGE_FLAVOR), AaptPackageResources.RESOURCE_APK_PATH_FORMAT));
    Date dosEpoch = new Date(ZipUtil.dosToJavaTime(ZipConstants.DOS_FAKE_TIME));
    try (ZipInputStream is = new ZipInputStream(new FileInputStream(aaptOutput.toFile()))) {
        for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
            assertThat(entry.getName(), new Date(entry.getTime()), Matchers.equalTo(dosEpoch));
        }
    }
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) Date(java.util.Date) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 12 with ZipInputStream

use of java.util.zip.ZipInputStream in project buck by facebook.

the class ProjectFilesystemTest method testCreateZipIgnoresMtimes.

@Test
public void testCreateZipIgnoresMtimes() throws IOException {
    // Create a empty executable file.
    Path exe = tmp.newFile("foo");
    // Archive it into a zipfile using `ProjectFileSystem.createZip`.
    Path zipFile = tmp.getRoot().resolve("test.zip");
    filesystem.createZip(ImmutableList.of(exe), zipFile);
    // Iterate over each of the entries, expecting to see all zeros in the time fields.
    Date dosEpoch = new Date(ZipUtil.dosToJavaTime(ZipConstants.DOS_FAKE_TIME));
    try (ZipInputStream is = new ZipInputStream(Files.newInputStream(zipFile))) {
        for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
            assertEquals(entry.getName(), dosEpoch, new Date(entry.getTime()));
        }
    }
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) Date(java.util.Date) Test(org.junit.Test)

Example 13 with ZipInputStream

use of java.util.zip.ZipInputStream in project buck by facebook.

the class JarDirectoryStepTest method jarsShouldContainDirectoryEntries.

@Test
public void jarsShouldContainDirectoryEntries() throws IOException {
    Path zipup = folder.newFolder("dir-zip");
    Path subdir = zipup.resolve("dir/subdir");
    Files.createDirectories(subdir);
    Files.write(subdir.resolve("a.txt"), "cake".getBytes());
    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(zipup), Paths.get("output.jar"), ImmutableSortedSet.of(zipup), /* main class */
    null, /* manifest file */
    null);
    ExecutionContext context = TestExecutionContext.newInstance();
    int returnCode = step.execute(context).getExitCode();
    assertEquals(0, returnCode);
    Path zip = zipup.resolve("output.jar");
    assertTrue(Files.exists(zip));
    // Iterate over each of the entries, expecting to see the directory names as entries.
    Set<String> expected = Sets.newHashSet("dir/", "dir/subdir/");
    try (ZipInputStream is = new ZipInputStream(Files.newInputStream(zip))) {
        for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
            expected.remove(entry.getName());
        }
    }
    assertTrue("Didn't see entries for: " + expected, expected.isEmpty());
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ZipEntry(java.util.zip.ZipEntry) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 14 with ZipInputStream

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

the class DexDiffPatchInternal method patchDexFile.

/**
     * Generate patched dex file (May wrapped it by a jar if needed.)
     * @param baseApk
     *   OldApk.
     * @param patchPkg
     *   Patch package, it is also a zip file.
     * @param oldDexEntry
     *   ZipEntry of old dex.
     * @param patchFileEntry
     *   ZipEntry of patch file. (also ends with .dex) This could be null.
     * @param patchInfo
     *   Parsed patch info from package-meta.txt
     * @param patchedDexFile
     *   Patched dex file, may be a jar.
     *
     * <b>Notice: patchFileEntry and smallPatchInfoFile cannot both be null.</b>
     *
     * @throws IOException
     */
private static void patchDexFile(ZipFile baseApk, ZipFile patchPkg, ZipEntry oldDexEntry, ZipEntry patchFileEntry, ShareDexDiffPatchInfo patchInfo, File patchedDexFile) throws IOException {
    InputStream oldDexStream = null;
    InputStream patchFileStream = null;
    try {
        oldDexStream = new BufferedInputStream(baseApk.getInputStream(oldDexEntry));
        patchFileStream = (patchFileEntry != null ? new BufferedInputStream(patchPkg.getInputStream(patchFileEntry)) : null);
        final boolean isRawDexFile = SharePatchFileUtil.isRawDexFile(patchInfo.rawName);
        if (!isRawDexFile || patchInfo.isJarMode) {
            ZipOutputStream zos = null;
            try {
                zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(patchedDexFile)));
                zos.putNextEntry(new ZipEntry(ShareConstants.DEX_IN_JAR));
                // Old dex is not a raw dex file.
                if (!isRawDexFile) {
                    ZipInputStream zis = null;
                    try {
                        zis = new ZipInputStream(oldDexStream);
                        ZipEntry entry;
                        while ((entry = zis.getNextEntry()) != null) {
                            if (ShareConstants.DEX_IN_JAR.equals(entry.getName()))
                                break;
                        }
                        if (entry == null) {
                            throw new TinkerRuntimeException("can't recognize zip dex format file:" + patchedDexFile.getAbsolutePath());
                        }
                        new DexPatchApplier(zis, patchFileStream).executeAndSaveTo(zos);
                    } finally {
                        SharePatchFileUtil.closeQuietly(zis);
                    }
                } else {
                    new DexPatchApplier(oldDexStream, patchFileStream).executeAndSaveTo(zos);
                }
                zos.closeEntry();
            } finally {
                SharePatchFileUtil.closeQuietly(zos);
            }
        } else {
            new DexPatchApplier(oldDexStream, patchFileStream).executeAndSaveTo(patchedDexFile);
        }
    } finally {
        SharePatchFileUtil.closeQuietly(oldDexStream);
        SharePatchFileUtil.closeQuietly(patchFileStream);
    }
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) ZipInputStream(java.util.zip.ZipInputStream) DexPatchApplier(com.tencent.tinker.commons.dexpatcher.DexPatchApplier) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) BufferedOutputStream(java.io.BufferedOutputStream)

Example 15 with ZipInputStream

use of java.util.zip.ZipInputStream in project Fairphone by Kwamecorp.

the class GappsInstallerHelper method unzip.

public void unzip(String filePath, String targetPath) {
    new File(targetPath).mkdirs();
    try {
        FileInputStream fin = new FileInputStream(filePath);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            Log.d(TAG, "Unzipping " + ze.getName());
            if (ze.isDirectory()) {
                _dirChecker(ze.getName(), targetPath);
            } else {
                FileOutputStream fout = new FileOutputStream(targetPath + ze.getName());
                byte[] buffer = new byte[2048];
                int count = 0;
                while ((count = zin.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }
                zin.closeEntry();
                fout.close();
            }
        }
        zin.close();
        fin.close();
    } catch (Exception e) {
        Log.e("Decompress", "unzip", e);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) TimeoutException(java.util.concurrent.TimeoutException) RootDeniedException(com.stericson.RootTools.exceptions.RootDeniedException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)354 ZipEntry (java.util.zip.ZipEntry)259 FileInputStream (java.io.FileInputStream)120 File (java.io.File)115 IOException (java.io.IOException)103 ByteArrayInputStream (java.io.ByteArrayInputStream)63 FileOutputStream (java.io.FileOutputStream)63 ByteArrayOutputStream (java.io.ByteArrayOutputStream)62 Test (org.junit.Test)56 InputStream (java.io.InputStream)53 BufferedInputStream (java.io.BufferedInputStream)47 ZipOutputStream (java.util.zip.ZipOutputStream)31 FileNotFoundException (java.io.FileNotFoundException)21 Path (java.nio.file.Path)20 HashMap (java.util.HashMap)19 BufferedOutputStream (java.io.BufferedOutputStream)18 ArrayList (java.util.ArrayList)18 ZipFile (java.util.zip.ZipFile)18 OutputStream (java.io.OutputStream)17 GZIPInputStream (java.util.zip.GZIPInputStream)14