Search in sources :

Example 66 with ZipException

use of java.util.zip.ZipException in project j2objc by google.

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 67 with ZipException

use of java.util.zip.ZipException in project j2objc by google.

the class OldZipInputStreamTest method test_read$BII.

public void test_read$BII() throws Exception {
    byte[] rbuf;
    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);
    zis1.getNextEntry();
    zis1.getNextEntry();
    rbuf = new byte[100];
    try {
        zis1.read(rbuf, 10, 90);
        fail("ZipException expected");
    } catch (ZipException ee) {
    // expected
    }
    try {
        // Android throws exception here, already!
        zis1.close();
        // But RI here, only!
        zis1.read(rbuf, 10, 90);
        fail("IOException expected");
    } catch (IOException 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 68 with ZipException

use of java.util.zip.ZipException in project j2objc by google.

the class JarInputStreamTest method test_closeAfterException.

public void test_closeAfterException() throws Exception {
    File resources = Support_Resources.createTempFolder();
    Support_Resources.copyFile(resources, null, "Broken_entry.jar");
    InputStream is = Support_Resources.getStream("Broken_entry.jar");
    JarInputStream jis = new JarInputStream(is, false);
    jis.getNextEntry();
    try {
        jis.getNextEntry();
        fail("ZipException expected");
    } catch (ZipException ee) {
    // expected
    }
    jis.close();
    try {
        jis.getNextEntry();
        fail("IOException expected");
    } catch (IOException ee) {
    // expected
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) File(java.io.File)

Example 69 with ZipException

use of java.util.zip.ZipException in project j2objc by google.

the class GenerationBatch method processJarFile.

private void processJarFile(String filename) {
    File f = findJarFile(filename);
    if (f == null) {
        ErrorUtil.error("No such file: " + filename);
        return;
    }
    // don't support Java-like source paths.
    if (options.emitLineDirectives() && options.isVerbose()) {
        ErrorUtil.warning("source debugging of jar files is not supported: " + filename);
    }
    GenerationUnit combinedUnit = null;
    if (globalCombinedUnit != null) {
        combinedUnit = globalCombinedUnit;
    } else if (options.getHeaderMap().combineSourceJars()) {
        combinedUnit = GenerationUnit.newCombinedJarUnit(filename, options);
    }
    try {
        ZipFile zfile = new ZipFile(f);
        try {
            boolean containsJavaFile = false;
            Enumeration<? extends ZipEntry> enumerator = zfile.entries();
            File tempDir = FileUtil.createTempDir(J2OBJC_TEMP_DIR_PREFIX);
            String tempDirPath = tempDir.getAbsolutePath();
            options.fileUtil().addTempDir(tempDirPath);
            options.fileUtil().appendSourcePath(tempDirPath);
            while (enumerator.hasMoreElements()) {
                ZipEntry entry = enumerator.nextElement();
                String internalPath = entry.getName();
                if (internalPath.endsWith(".java") || (options.translateClassfiles() && internalPath.endsWith(".class"))) {
                    // Extract JAR file to a temporary directory
                    File outputFile = options.fileUtil().extractZipEntry(tempDir, zfile, entry);
                    InputFile newFile = new RegularInputFile(outputFile.getAbsolutePath(), internalPath);
                    if (combinedUnit != null) {
                        inputs.add(new ProcessingContext(newFile, combinedUnit));
                    } else {
                        addExtractedJarSource(newFile, filename, internalPath);
                    }
                    containsJavaFile = true;
                }
            }
            if (!options.translateClassfiles() && !containsJavaFile) {
                ErrorUtil.error(filename + " does not contain any Java source files.");
            }
        } finally {
            // Also closes input stream.
            zfile.close();
        }
    } catch (ZipException e) {
        // Also catches JarExceptions
        logger.fine(e.getMessage());
        ErrorUtil.error("Error reading file " + filename + " as a zip or jar file.");
    } catch (IOException e) {
        ErrorUtil.error(e.getMessage());
    }
}
Also used : GenerationUnit(com.google.devtools.j2objc.gen.GenerationUnit) ZipFile(java.util.zip.ZipFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) InputFile(com.google.devtools.j2objc.file.InputFile) RegularInputFile(com.google.devtools.j2objc.file.RegularInputFile)

Example 70 with ZipException

use of java.util.zip.ZipException in project dhis2-core by dhis2.

the class JCloudsAppStorageService method installApp.

@Override
public App installApp(File file, String filename, Cache<App> appCache) {
    App app = new App();
    log.info("Installing new app: " + filename);
    try (ZipFile zip = new ZipFile(file)) {
        // -----------------------------------------------------------------
        // Parse ZIP file and it's manifest.webapp file.
        // -----------------------------------------------------------------
        ZipEntry entry = zip.getEntry(MANIFEST_FILENAME);
        if (entry == null) {
            log.error("Failed to install app: Missing manifest.webapp in zip");
            app.setAppState(AppStatus.MISSING_MANIFEST);
            return app;
        }
        InputStream inputStream = zip.getInputStream(entry);
        app = jsonMapper.readValue(inputStream, App.class);
        app.setFolderName(APPS_DIR + File.separator + filename.substring(0, filename.lastIndexOf('.')));
        app.setAppStorageSource(AppStorageSource.JCLOUDS);
        if (!this.validateApp(app, appCache)) {
            return app;
        }
        String namespace = app.getActivities().getDhis().getNamespace();
        // -----------------------------------------------------------------
        // Unzip the app
        // -----------------------------------------------------------------
        String dest = APPS_DIR + File.separator + filename.substring(0, filename.lastIndexOf('.'));
        zip.stream().forEach((Consumer<ZipEntry>) zipEntry -> {
            log.debug("Uploading zipEntry: " + zipEntry);
            try {
                InputStream input = zip.getInputStream(zipEntry);
                Blob blob = blobStore.blobBuilder(dest + File.separator + zipEntry.getName()).payload(input).contentLength(zipEntry.getSize()).build();
                blobStore.putBlob(config.container, blob);
                input.close();
            } catch (IOException e) {
                log.error("Unable to store app file '" + zipEntry.getName() + "'", e);
            }
        });
        log.info(String.format("" + "New app '%s' installed" + "\n\tInstall path: %s" + (namespace != null && !namespace.isEmpty() ? "\n\tNamespace reserved: %s" : ""), app.getName(), dest, namespace));
        // -----------------------------------------------------------------
        // Installation complete.
        // -----------------------------------------------------------------
        app.setAppState(AppStatus.OK);
        return app;
    } catch (ZipException e) {
        log.error("Failed to install app: Invalid ZIP format", e);
        app.setAppState(AppStatus.INVALID_ZIP_FORMAT);
    } catch (JsonParseException e) {
        log.error("Failed to install app: Invalid manifest.webapp", e);
        app.setAppState(AppStatus.INVALID_MANIFEST_JSON);
    } catch (IOException e) {
        log.error("Failed to install app: Could not save app", e);
        app.setAppState(AppStatus.INSTALLATION_FAILED);
    }
    return app;
}
Also used : HttpRequest(org.jclouds.http.HttpRequest) Arrays(java.util.Arrays) ConfigurationKey(org.hisp.dhis.external.conf.ConfigurationKey) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) LocalBlobRequestSigner(org.jclouds.blobstore.LocalBlobRequestSigner) PreDestroy(javax.annotation.PreDestroy) BlobStoreContext(org.jclouds.blobstore.BlobStoreContext) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Blob(org.jclouds.blobstore.domain.Blob) ZipFile(java.util.zip.ZipFile) URI(java.net.URI) ZipEntry(java.util.zip.ZipEntry) JsonParseException(com.fasterxml.jackson.core.JsonParseException) HttpResponseException(org.jclouds.http.HttpResponseException) Resource(org.springframework.core.io.Resource) Builder.prefix(org.jclouds.blobstore.options.ListContainerOptions.Builder.prefix) ZipException(java.util.zip.ZipException) RequestSigningUnsupported(org.jclouds.blobstore.internal.RequestSigningUnsupported) FileSystemResource(org.springframework.core.io.FileSystemResource) LocationBuilder(org.jclouds.domain.LocationBuilder) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Cache(org.hisp.dhis.cache.Cache) Credentials(org.jclouds.domain.Credentials) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Minutes(org.joda.time.Minutes) LocationManager(org.hisp.dhis.external.location.LocationManager) UrlResource(org.springframework.core.io.UrlResource) HashMap(java.util.HashMap) S3Constants(org.jclouds.s3.reference.S3Constants) Location(org.jclouds.domain.Location) ArrayList(java.util.ArrayList) AuthorizationException(org.jclouds.rest.AuthorizationException) Service(org.springframework.stereotype.Service) Properties(java.util.Properties) BlobStore(org.jclouds.blobstore.BlobStore) DhisConfigurationProvider(org.hisp.dhis.external.conf.DhisConfigurationProvider) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) File(java.io.File) ContextBuilder(org.jclouds.ContextBuilder) Consumer(java.util.function.Consumer) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) BlobRequestSigner(org.jclouds.blobstore.BlobRequestSigner) LocationScope(org.jclouds.domain.LocationScope) FilesystemConstants(org.jclouds.filesystem.reference.FilesystemConstants) StorageMetadata(org.jclouds.blobstore.domain.StorageMetadata) LocationManagerException(org.hisp.dhis.external.location.LocationManagerException) InputStream(java.io.InputStream) Blob(org.jclouds.blobstore.domain.Blob) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

ZipException (java.util.zip.ZipException)197 IOException (java.io.IOException)96 File (java.io.File)74 ZipEntry (java.util.zip.ZipEntry)67 ZipFile (java.util.zip.ZipFile)63 InputStream (java.io.InputStream)50 FileInputStream (java.io.FileInputStream)39 ZipInputStream (java.util.zip.ZipInputStream)26 FileOutputStream (java.io.FileOutputStream)23 BufferedInputStream (java.io.BufferedInputStream)22 JarFile (java.util.jar.JarFile)21 FileNotFoundException (java.io.FileNotFoundException)19 JarEntry (java.util.jar.JarEntry)19 ArrayList (java.util.ArrayList)18 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ZipOutputStream (java.util.zip.ZipOutputStream)15 URL (java.net.URL)14 GZIPInputStream (java.util.zip.GZIPInputStream)12 BufferedOutputStream (java.io.BufferedOutputStream)8 RandomAccessFile (java.io.RandomAccessFile)8