Search in sources :

Example 11 with ZipException

use of java.util.zip.ZipException in project robovm by robovm.

the class ZipFileTest method testDuplicateEntries.

/**
     * Make sure we don't fail silently for duplicate entries.
     * b/8219321
     */
public void testDuplicateEntries() throws Exception {
    String name1 = "test_file_name1";
    String name2 = "test_file_name2";
    // Create the good zip file.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream out = new ZipOutputStream(baos);
    out.putNextEntry(new ZipEntry(name2));
    out.closeEntry();
    out.putNextEntry(new ZipEntry(name1));
    out.closeEntry();
    out.close();
    // Rewrite one of the filenames.
    byte[] buffer = baos.toByteArray();
    replaceBytes(buffer, name2.getBytes(), name1.getBytes());
    // Write the result to a file.
    File badZip = createTemporaryZipFile();
    writeBytes(badZip, buffer);
    // Check that we refuse to load the modified file.
    try {
        ZipFile bad = new ZipFile(badZip);
        fail();
    } catch (ZipException expected) {
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 12 with ZipException

use of java.util.zip.ZipException in project robovm by robovm.

the class OldZipExceptionTest method testZipException.

public void testZipException() {
    ZipException zz = new ZipException();
    assertEquals(zz.getMessage(), null);
}
Also used : ZipException(java.util.zip.ZipException)

Example 13 with ZipException

use of java.util.zip.ZipException in project robovm by robovm.

the class OldZipInputStreamTest method test_closeEntry.

public void test_closeEntry() throws Exception {
    zis.getNextEntry();
    zis.closeEntry();
    zis.getNextEntry();
    zis.close();
    try {
        zis.closeEntry();
        fail("IOException expected");
    } catch (IOException ee) {
    // expected
    }
    File resources = Support_Resources.createTempFolder();
    Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
    FileInputStream fis = new FileInputStream(new File(resources, "Broken_manifest.jar"));
    ZipInputStream zis1 = new ZipInputStream(fis);
    try {
        for (int i = 0; i < 6; i++) {
            zis1.getNextEntry();
            zis1.closeEntry();
        }
        fail("ZipException expected");
    } catch (ZipException ee) {
    // expected
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 14 with ZipException

use of java.util.zip.ZipException in project robovm by robovm.

the class JarFileTest method test_getInputStreamLjava_util_jar_JarEntry.

/**
     * @throws IOException
     * java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry)
     */
public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException {
    File localFile = null;
    try {
        Support_Resources.copyFile(resources, null, jarName);
        localFile = new File(resources, jarName);
    } catch (Exception e) {
        fail("Failed to create local file: " + e);
    }
    byte[] b = new byte[1024];
    try {
        JarFile jf = new JarFile(localFile);
        java.io.InputStream is = jf.getInputStream(jf.getEntry(entryName));
        // BEGIN android-removed
        // jf.close();
        // END android-removed
        assertTrue("Returned invalid stream", is.available() > 0);
        int r = is.read(b, 0, 1024);
        is.close();
        StringBuffer sb = new StringBuffer(r);
        for (int i = 0; i < r; i++) {
            sb.append((char) (b[i] & 0xff));
        }
        String contents = sb.toString();
        assertTrue("Incorrect stream read", contents.indexOf("bar") > 0);
        // BEGIN android-added
        jf.close();
    // END android-added
    } catch (Exception e) {
        fail("Exception during test: " + e.toString());
    }
    try {
        JarFile jf = new JarFile(localFile);
        InputStream in = jf.getInputStream(new JarEntry("invalid"));
        assertNull("Got stream for non-existent entry", in);
    } catch (Exception e) {
        fail("Exception during test 2: " + e);
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        JarFile jf = new JarFile(signedFile);
        JarEntry jre = new JarEntry("foo/bar/A.class");
        jf.getInputStream(jre);
    // InputStream returned in any way, exception can be thrown in case
    // of reading from this stream only.
    // fail("Should throw ZipException");
    } catch (ZipException ee) {
    // expected
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        JarFile jf = new JarFile(signedFile);
        JarEntry jre = new JarEntry("foo/bar/A.class");
        jf.close();
        jf.getInputStream(jre);
        // InputStream returned in any way, exception can be thrown in case
        // of reading from this stream only.
        // The same for IOException
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException ee) {
    // expected
    }
}
Also used : InputStream(java.io.InputStream) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream)

Example 15 with ZipException

use of java.util.zip.ZipException in project bazel by bazelbuild.

the class FdoSupport method extractFdoZip.

/**
   * Extracts the FDO zip file and collects data from it that's needed during analysis.
   *
   * <p>When an {@code --fdo_optimize} compile is requested, unpacks the given
   * FDO gcda zip file into a clean working directory under execRoot.
   *
   * @throws FdoException if the FDO ZIP contains a file of unknown type
   */
private static FdoZipContents extractFdoZip(FdoMode fdoMode, LipoMode lipoMode, Path execRoot, Path fdoProfile, PathFragment fdoRootExecPath, String productName) throws IOException, FdoException {
    // The execRoot != null case is only there for testing. We cannot provide a real ZIP file in
    // tests because ZipFileSystem does not work with a ZIP on an in-memory file system.
    // IMPORTANT: Keep in sync with #declareSkyframeDependencies to avoid incrementality issues.
    ImmutableSet<PathFragment> gcdaFiles = ImmutableSet.of();
    ImmutableMultimap<PathFragment, PathFragment> imports = ImmutableMultimap.of();
    if (fdoProfile != null && execRoot != null) {
        Path fdoDirPath = execRoot.getRelative(fdoRootExecPath);
        FileSystemUtils.deleteTreesBelow(fdoDirPath);
        FileSystemUtils.createDirectoryAndParents(fdoDirPath);
        if (fdoMode == FdoMode.AUTO_FDO) {
            if (lipoMode != LipoMode.OFF) {
                imports = readAutoFdoImports(getAutoFdoImportsPath(fdoProfile));
            }
            FileSystemUtils.ensureSymbolicLink(execRoot.getRelative(getAutoProfilePath(fdoProfile, fdoRootExecPath)), fdoProfile);
        } else if (fdoMode == FdoMode.LLVM_FDO) {
            FileSystemUtils.ensureSymbolicLink(execRoot.getRelative(getLLVMProfilePath(fdoProfile, fdoRootExecPath)), fdoProfile);
        } else {
            Path zipFilePath = new ZipFileSystem(fdoProfile).getRootDirectory();
            String outputSymlinkName = productName + "-out";
            if (!zipFilePath.getRelative(outputSymlinkName).isDirectory()) {
                throw new ZipException("FDO zip files must be zipped directly above '" + outputSymlinkName + "' for the compiler to find the profile");
            }
            ImmutableSet.Builder<PathFragment> gcdaFilesBuilder = ImmutableSet.builder();
            ImmutableMultimap.Builder<PathFragment, PathFragment> importsBuilder = ImmutableMultimap.builder();
            extractFdoZipDirectory(zipFilePath, fdoDirPath, gcdaFilesBuilder, importsBuilder);
            gcdaFiles = gcdaFilesBuilder.build();
            imports = importsBuilder.build();
        }
    }
    return new FdoZipContents(gcdaFiles, imports);
}
Also used : RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) ZipException(java.util.zip.ZipException) ZipFileSystem(com.google.devtools.build.lib.vfs.ZipFileSystem)

Aggregations

ZipException (java.util.zip.ZipException)188 IOException (java.io.IOException)89 File (java.io.File)71 ZipEntry (java.util.zip.ZipEntry)66 ZipFile (java.util.zip.ZipFile)62 InputStream (java.io.InputStream)45 FileInputStream (java.io.FileInputStream)37 ZipInputStream (java.util.zip.ZipInputStream)26 BufferedInputStream (java.io.BufferedInputStream)22 FileOutputStream (java.io.FileOutputStream)21 JarFile (java.util.jar.JarFile)21 JarEntry (java.util.jar.JarEntry)19 FileNotFoundException (java.io.FileNotFoundException)18 ArrayList (java.util.ArrayList)17 ZipOutputStream (java.util.zip.ZipOutputStream)15 URL (java.net.URL)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 GZIPInputStream (java.util.zip.GZIPInputStream)10 BufferedOutputStream (java.io.BufferedOutputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7