Search in sources :

Example 6 with ZipException

use of java.util.zip.ZipException in project intellij-community by JetBrains.

the class Digester method digestZipFile.

public static long digestZipFile(File file) throws IOException {
    ZipFile zipFile;
    try {
        zipFile = new ZipFile(file);
    } catch (ZipException e) {
        // This was not a zip file...
        return digestRegularFile(file, false);
    }
    try {
        List<ZipEntry> sorted = new ArrayList<>();
        Enumeration<? extends ZipEntry> temp = zipFile.entries();
        while (temp.hasMoreElements()) {
            ZipEntry each = temp.nextElement();
            if (!each.isDirectory()) {
                sorted.add(each);
            }
        }
        Collections.sort(sorted, Comparator.comparing(ZipEntry::getName));
        CRC32 crc = new CRC32();
        for (ZipEntry each : sorted) {
            try (InputStream in = zipFile.getInputStream(each)) {
                doDigestStream(in, crc);
            }
        }
        return crc.getValue();
    } finally {
        zipFile.close();
    }
}
Also used : ZipFile(java.util.zip.ZipFile) CRC32(java.util.zip.CRC32) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException)

Example 7 with ZipException

use of java.util.zip.ZipException in project gerrit by GerritCodeReview.

the class InstallPlugin method apply.

@Override
public Response<PluginInfo> apply(TopLevelResource resource, Input input) throws BadRequestException, MethodNotAllowedException, IOException {
    if (!loader.isRemoteAdminEnabled()) {
        throw new MethodNotAllowedException("remote installation is disabled");
    }
    try {
        try (InputStream in = openStream(input)) {
            String pluginName = loader.installPluginFromStream(name, in);
            ListPlugins.PluginInfo info = new ListPlugins.PluginInfo(loader.get(pluginName));
            return created ? Response.created(info) : Response.ok(info);
        }
    } catch (PluginInstallException e) {
        StringWriter buf = new StringWriter();
        buf.write(String.format("cannot install %s", name));
        if (e.getCause() instanceof ZipException) {
            buf.write(": ");
            buf.write(e.getCause().getMessage());
        } else {
            buf.write(":\n");
            PrintWriter pw = new PrintWriter(buf);
            e.printStackTrace(pw);
            pw.flush();
        }
        throw new BadRequestException(buf.toString());
    }
}
Also used : PluginInfo(com.google.gerrit.server.plugins.ListPlugins.PluginInfo) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PluginInfo(com.google.gerrit.server.plugins.ListPlugins.PluginInfo) ZipException(java.util.zip.ZipException) PrintWriter(java.io.PrintWriter)

Example 8 with ZipException

use of java.util.zip.ZipException in project intellij-community by JetBrains.

the class GradleImportingTestCase method configureWrapper.

private void configureWrapper() throws IOException, URISyntaxException {
    final URI distributionUri = new DistributionLocator().getDistributionFor(GradleVersion.version(gradleVersion));
    myProjectSettings.setDistributionType(DistributionType.DEFAULT_WRAPPED);
    final VirtualFile wrapperJarFrom = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(wrapperJar());
    assert wrapperJarFrom != null;
    final VirtualFile wrapperJarFromTo = createProjectSubFile("gradle/wrapper/gradle-wrapper.jar");
    new WriteAction() {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            wrapperJarFromTo.setBinaryContent(wrapperJarFrom.contentsToByteArray());
        }
    }.execute().throwException();
    Properties properties = new Properties();
    properties.setProperty("distributionBase", "GRADLE_USER_HOME");
    properties.setProperty("distributionPath", "wrapper/dists");
    properties.setProperty("zipStoreBase", "GRADLE_USER_HOME");
    properties.setProperty("zipStorePath", "wrapper/dists");
    properties.setProperty("distributionUrl", distributionUri.toString());
    StringWriter writer = new StringWriter();
    properties.store(writer, null);
    createProjectSubFile("gradle/wrapper/gradle-wrapper.properties", writer.toString());
    WrapperConfiguration wrapperConfiguration = GradleUtil.getWrapperConfiguration(getProjectPath());
    PathAssembler.LocalDistribution localDistribution = new PathAssembler(StartParameter.DEFAULT_GRADLE_USER_HOME).getDistribution(wrapperConfiguration);
    File zip = localDistribution.getZipFile();
    try {
        if (zip.exists()) {
            ZipFile zipFile = new ZipFile(zip);
            zipFile.close();
        }
    } catch (ZipException e) {
        e.printStackTrace();
        System.out.println("Corrupted file will be removed: " + zip.getPath());
        FileUtil.delete(zip);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DistributionLocator(org.jetbrains.plugins.gradle.tooling.builder.AbstractModelBuilderTest.DistributionLocator) WriteAction(com.intellij.openapi.application.WriteAction) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) Properties(java.util.Properties) WrapperConfiguration(org.gradle.wrapper.WrapperConfiguration) URI(java.net.URI) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) PathAssembler(org.gradle.wrapper.PathAssembler) StringWriter(java.io.StringWriter) ZipFile(java.util.zip.ZipFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 9 with ZipException

use of java.util.zip.ZipException in project phoenix by apache.

the class ResourceList method getResourcesFromJarFile.

// Visible for testing
Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) {
    final List<String> retVal = new ArrayList<>();
    ZipFile zf;
    try {
        zf = new ZipFile(file);
    } catch (FileNotFoundException e) {
        // Gracefully handle a jar listed on the classpath that doesn't actually exist.
        return Collections.emptyList();
    } catch (final ZipException e) {
        throw new Error(e);
    } catch (final IOException e) {
        throw new Error(e);
    }
    final Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        final ZipEntry ze = (ZipEntry) e.nextElement();
        final String fileName = ze.getName();
        final boolean accept = pattern.matcher(fileName).matches();
        logger.trace("fileName:" + fileName);
        logger.trace("File:" + file.toString());
        logger.trace("Match:" + accept);
        if (accept) {
            logger.trace("Adding File from Jar: " + fileName);
            retVal.add("/" + fileName);
        }
    }
    try {
        zf.close();
    } catch (final IOException e1) {
        throw new Error(e1);
    }
    return retVal;
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException)

Example 10 with ZipException

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

the class ZipFileTest method testSTORED.

public void testSTORED() throws IOException {
    ZipOutputStream out = createZipOutputStream(createTemporaryZipFile());
    CRC32 crc = new CRC32();
    // Missing CRC, size, and compressed size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Missing CRC and compressed size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        ze.setSize(0);
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Missing CRC and size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        ze.setSize(0);
        ze.setCompressedSize(0);
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Missing size and compressed size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        ze.setCrc(crc.getValue());
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Missing size is copied from compressed size.
    {
        ZipEntry ze = new ZipEntry("okay1");
        ze.setMethod(ZipEntry.STORED);
        ze.setCrc(crc.getValue());
        assertEquals(-1, ze.getSize());
        assertEquals(-1, ze.getCompressedSize());
        ze.setCompressedSize(0);
        assertEquals(-1, ze.getSize());
        assertEquals(0, ze.getCompressedSize());
        out.putNextEntry(ze);
        assertEquals(0, ze.getSize());
        assertEquals(0, ze.getCompressedSize());
    }
    // Missing compressed size is copied from size.
    {
        ZipEntry ze = new ZipEntry("okay2");
        ze.setMethod(ZipEntry.STORED);
        ze.setCrc(crc.getValue());
        assertEquals(-1, ze.getSize());
        assertEquals(-1, ze.getCompressedSize());
        ze.setSize(0);
        assertEquals(0, ze.getSize());
        assertEquals(-1, ze.getCompressedSize());
        out.putNextEntry(ze);
        assertEquals(0, ze.getSize());
        assertEquals(0, ze.getCompressedSize());
    }
    // Mismatched size and compressed size => failure.
    try {
        ZipEntry ze = new ZipEntry("a");
        ze.setMethod(ZipEntry.STORED);
        ze.setCrc(crc.getValue());
        ze.setCompressedSize(1);
        ze.setSize(0);
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
    // Everything present => success.
    ZipEntry ze = new ZipEntry("okay");
    ze.setMethod(ZipEntry.STORED);
    ze.setCrc(crc.getValue());
    ze.setSize(0);
    ze.setCompressedSize(0);
    out.putNextEntry(ze);
    out.close();
}
Also used : CRC32(java.util.zip.CRC32) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException)

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