Search in sources :

Example 46 with ZipException

use of java.util.zip.ZipException in project mdw-designer by CenturyLinkCloud.

the class ProjectUpdater method addWebLibs.

/**
 * Update WEB-INF/lib jars in MDWWeb.war with contents of zip from the MDW
 * Release site. (Assumes a clean MDWWeb.war without any extensions.)
 */
public void addWebLibs(String zipFile, String webAppWarName, List<DescriptorUpdater> descriptorUpdaters, boolean doDownload, boolean deleteZip, IProgressMonitor monitor) throws InvocationTargetException, InterruptedException, CoreException, IOException {
    IFile webAppWar;
    if (workflowProject.isCloudProject()) {
        project = workflowProject.getSourceProject();
        localFolder = workflowProject.getSourceProject().getFolder(MdwPlugin.getSettings().getTempResourceLocation());
        IFolder deployEarFolder = workflowProject.getSourceProject().getFolder(new Path("deploy/ear"));
        webAppWar = deployEarFolder.getFile(webAppWarName);
    } else {
        project = workflowProject.getEarProject();
        localFolder = workflowProject.getEarContentFolder();
        webAppWar = localFolder.getFile(webAppWarName);
    }
    if (!webAppWar.exists())
        // may not exist in some scenarios
        return;
    file = project.getFile(localFolder.getProjectRelativePath().toString() + "/" + zipFile);
    IFile newWebAppWar = localFolder.getFile(webAppWarName + ".tmp");
    ZipFile zip = null;
    JarFile warArchive = null;
    JarOutputStream jarOut = null;
    BufferedOutputStream bufferedOut = null;
    monitor.beginTask("Building " + webAppWarName, 200);
    try {
        monitor.worked(5);
        if (doDownload)
            PluginUtil.downloadIntoProject(project, getFileUrl(), localFolder, file, "Download", // 75 ticks
            monitor);
        else
            monitor.worked(75);
        File outFile = new File(newWebAppWar.getLocationURI());
        bufferedOut = new BufferedOutputStream(new FileOutputStream(outFile));
        jarOut = new JarOutputStream(bufferedOut);
        int read = 0;
        byte[] buf = new byte[1024];
        monitor.subTask("Add existing entries to WAR file");
        SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 50);
        // lots of
        subMonitor.beginTask("Add existing entries", 500);
        // entries
        subMonitor.worked(5);
        warArchive = new JarFile(new File(webAppWar.getLocationURI()));
        for (Enumeration<?> entriesEnum = warArchive.entries(); entriesEnum.hasMoreElements(); ) {
            JarEntry jarEntry = (JarEntry) entriesEnum.nextElement();
            DescriptorUpdater dUpdater = null;
            if (descriptorUpdaters != null) {
                for (DescriptorUpdater descriptorUpdater : descriptorUpdaters) {
                    if (descriptorUpdater.getFilePath().equals(jarEntry.getName())) {
                        dUpdater = descriptorUpdater;
                        break;
                    }
                }
            }
            InputStream warIn = warArchive.getInputStream(jarEntry);
            if (dUpdater == null) {
                // straight pass-through
                jarOut.putNextEntry(jarEntry);
                while ((read = warIn.read(buf)) != -1) jarOut.write(buf, 0, read);
            } else {
                jarOut.putNextEntry(new JarEntry(jarEntry.getName()));
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                // files)
                while ((read = warIn.read(buf)) != -1) baos.write(buf, 0, read);
                baos.flush();
                byte[] contents = dUpdater.processContents(new String(baos.toByteArray()), monitor).getBytes();
                jarOut.write(contents, 0, contents.length);
            }
            warIn.close();
            subMonitor.worked(1);
        }
        subMonitor.done();
        monitor.subTask("Add new entries to WAR file");
        subMonitor = new SubProgressMonitor(monitor, 50);
        // potentially lots of
        subMonitor.beginTask("Add new entries", 500);
        // entries
        subMonitor.worked(5);
        zip = new ZipFile(new File(file.getLocationURI()));
        for (Enumeration<?> entriesEnum = zip.entries(); entriesEnum.hasMoreElements(); ) {
            ZipEntry zipEntry = (ZipEntry) entriesEnum.nextElement();
            try {
                jarOut.putNextEntry(zipEntry);
            } catch (ZipException ex) {
                // ignore duplicate entries
                if (ex.getMessage().startsWith("duplicate entry:"))
                    PluginMessages.log(ex.getMessage());
                else
                    throw ex;
            }
            InputStream zipIn = zip.getInputStream(zipEntry);
            while ((read = zipIn.read(buf)) != -1) jarOut.write(buf, 0, read);
            zipIn.close();
            subMonitor.worked(1);
        }
        subMonitor.done();
    } finally {
        if (zip != null)
            zip.close();
        if (warArchive != null)
            warArchive.close();
        if (jarOut != null)
            jarOut.close();
        if (bufferedOut != null)
            bufferedOut.close();
        SubProgressMonitor subMon = monitor.isCanceled() ? null : new SubProgressMonitor(monitor, 25);
        monitor.subTask("Cleaning up");
        if (deleteZip) {
            file.refreshLocal(1, subMon);
            file.delete(true, subMon);
        }
        webAppWar.refreshLocal(1, subMon);
        if (webAppWar.exists())
            webAppWar.delete(true, subMon);
        newWebAppWar.refreshLocal(1, subMon);
        if (newWebAppWar.exists())
            newWebAppWar.move(webAppWar.getFullPath(), true, subMon);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) DescriptorUpdater(com.centurylink.mdw.plugin.project.extensions.DescriptorUpdater) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) ZipException(java.util.zip.ZipException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder)

Example 47 with ZipException

use of java.util.zip.ZipException in project phosphor by gmu-swe.

the class Instrumenter method processZip.

/**
 * Handle Jar file, Zip file and War file
 */
public static void processZip(File f, File outputDir) {
    try {
        ZipFile zip = new ZipFile(f);
        ZipOutputStream zos = null;
        zos = new ZipOutputStream(new FileOutputStream(outputDir.getPath() + File.separator + f.getName()));
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            if (e.getName().endsWith(".class")) {
                if (ANALYZE_ONLY)
                    analyzeClass(zip.getInputStream(e));
                else {
                    try {
                        ZipEntry outEntry = new ZipEntry(e.getName());
                        zos.putNextEntry(outEntry);
                        byte[] clazz = instrumentClass(f.getAbsolutePath(), zip.getInputStream(e), true);
                        if (clazz == null) {
                            System.out.println("Failed to instrument " + e.getName() + " in " + f.getName());
                            InputStream is = zip.getInputStream(e);
                            byte[] buffer = new byte[1024];
                            while (true) {
                                int count = is.read(buffer);
                                if (count == -1)
                                    break;
                                zos.write(buffer, 0, count);
                            }
                            is.close();
                        } else
                            zos.write(clazz);
                        zos.closeEntry();
                    } catch (ZipException ex) {
                        ex.printStackTrace();
                        continue;
                    }
                }
            } else if (e.getName().endsWith(".jar")) {
                ZipEntry outEntry = new ZipEntry(e.getName());
                Random r = new Random();
                String markFileName = Long.toOctalString(System.currentTimeMillis()) + Integer.toOctalString(r.nextInt(10000)) + e.getName().replace("/", "");
                File tmp = new File("/tmp/" + markFileName);
                if (tmp.exists())
                    tmp.delete();
                FileOutputStream fos = new FileOutputStream(tmp);
                byte[] buf = new byte[1024];
                int len;
                InputStream is = zip.getInputStream(e);
                while ((len = is.read(buf)) > 0) {
                    fos.write(buf, 0, len);
                }
                is.close();
                fos.close();
                File tmp2 = new File("/tmp/tmp2");
                if (!tmp2.exists())
                    tmp2.mkdir();
                processZip(tmp, tmp2);
                tmp.delete();
                zos.putNextEntry(outEntry);
                is = new FileInputStream("/tmp/tmp2/" + markFileName);
                byte[] buffer = new byte[1024];
                while (true) {
                    int count = is.read(buffer);
                    if (count == -1)
                        break;
                    zos.write(buffer, 0, count);
                }
                is.close();
                zos.closeEntry();
            } else {
                ZipEntry outEntry = new ZipEntry(e.getName());
                if (e.isDirectory()) {
                    try {
                        zos.putNextEntry(outEntry);
                        zos.closeEntry();
                    } catch (ZipException exxxx) {
                        System.out.println("Ignoring exception: " + exxxx.getMessage());
                    }
                } else if (e.getName().startsWith("META-INF") && (e.getName().endsWith(".SF") || e.getName().endsWith(".RSA"))) {
                // don't copy this
                } else if (e.getName().equals("META-INF/MANIFEST.MF")) {
                    Scanner s = new Scanner(zip.getInputStream(e));
                    zos.putNextEntry(outEntry);
                    String curPair = "";
                    while (s.hasNextLine()) {
                        String line = s.nextLine();
                        if (line.equals("")) {
                            curPair += "\n";
                            if (!curPair.contains("SHA1-Digest:"))
                                zos.write(curPair.getBytes());
                            curPair = "";
                        } else {
                            curPair += line + "\n";
                        }
                    }
                    s.close();
                    // Jar file is different from Zip file. :)
                    if (f.getName().endsWith(".zip"))
                        zos.write("\n".getBytes());
                    zos.closeEntry();
                } else {
                    try {
                        zos.putNextEntry(outEntry);
                        InputStream is = zip.getInputStream(e);
                        byte[] buffer = new byte[1024];
                        while (true) {
                            int count = is.read(buffer);
                            if (count == -1)
                                break;
                            zos.write(buffer, 0, count);
                        }
                        is.close();
                        zos.closeEntry();
                    } catch (ZipException ex) {
                        if (!ex.getMessage().contains("duplicate entry")) {
                            ex.printStackTrace();
                            System.out.println("Ignoring above warning from improper source zip...");
                        }
                    }
                }
            }
        }
        if (zos != null)
            zos.close();
        zip.close();
    } catch (Exception e) {
        System.err.println("Unable to process zip/jar: " + f.getAbsolutePath());
        e.printStackTrace();
        File dest = new File(outputDir.getPath() + File.separator + f.getName());
        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(f).getChannel();
            destination = new FileOutputStream(dest).getChannel();
            destination.transferFrom(source, 0, source.size());
        } catch (Exception ex) {
            System.err.println("Unable to copy zip/jar: " + f.getAbsolutePath());
            ex.printStackTrace();
        } finally {
            if (source != null) {
                try {
                    source.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
            if (destination != null) {
                try {
                    destination.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
}
Also used : Scanner(java.util.Scanner) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ZipException(java.util.zip.ZipException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ZipFile(java.util.zip.ZipFile) Random(java.util.Random) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 48 with ZipException

use of java.util.zip.ZipException in project LibreraReader by foobnix.

the class ZipFile method getInputStream.

/**
 * Returns an InputStream for reading the contents of the given entry.
 *
 * @param ze the entry to get the stream for.
 * @return a stream to read the entry from.
 * @throws IOException if unable to create an input stream from the zipentry
 */
public InputStream getInputStream(final ZipArchiveEntry ze) throws IOException {
    if (!(ze instanceof Entry)) {
        return null;
    }
    // cast validity is checked just above
    ZipUtil.checkRequestedFeatures(ze);
    final long start = ze.getDataOffset();
    // doesn't get closed if the method is not supported - which
    // should never happen because of the checkRequestedFeatures
    // call above
    final InputStream is = // NOSONAR
    new BufferedInputStream(createBoundedInputStream(start, ze.getCompressedSize()));
    switch(ZipMethod.getMethodByCode(ze.getMethod())) {
        case STORED:
            return is;
        case UNSHRINKING:
            return new UnshrinkingInputStream(is);
        case IMPLODING:
            return new ExplodingInputStream(ze.getGeneralPurposeBit().getSlidingDictionarySize(), ze.getGeneralPurposeBit().getNumberOfShannonFanoTrees(), is);
        case DEFLATED:
            final Inflater inflater = new Inflater(true);
            // https://docs.oracle.com/javase/7/docs/api/java/util/zip/Inflater.html#Inflater(boolean)
            return new InflaterInputStream(new SequenceInputStream(is, new ByteArrayInputStream(ONE_ZERO_BYTE)), inflater) {

                @Override
                public void close() throws IOException {
                    try {
                        super.close();
                    } finally {
                        inflater.end();
                    }
                }
            };
        case BZIP2:
            return new BZip2CompressorInputStream(is);
        case ENHANCED_DEFLATED:
            return new Deflate64CompressorInputStream(is);
        case AES_ENCRYPTED:
        case EXPANDING_LEVEL_1:
        case EXPANDING_LEVEL_2:
        case EXPANDING_LEVEL_3:
        case EXPANDING_LEVEL_4:
        case JPEG:
        case LZMA:
        case PKWARE_IMPLODING:
        case PPMD:
        case TOKENIZATION:
        case UNKNOWN:
        case WAVPACK:
        case XZ:
        default:
            throw new ZipException("Found unsupported compression method " + ze.getMethod());
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Deflate64CompressorInputStream(org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ZipException(java.util.zip.ZipException) SequenceInputStream(java.io.SequenceInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Inflater(java.util.zip.Inflater) Deflate64CompressorInputStream(org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream)

Example 49 with ZipException

use of java.util.zip.ZipException in project cdap by caskdata.

the class ArtifactInspector method inspectApplications.

private ArtifactClasses.Builder inspectApplications(Id.Artifact artifactId, ArtifactClasses.Builder builder, Location artifactLocation, ClassLoader artifactClassLoader) throws IOException, InvalidArtifactException {
    // right now we force users to include the application main class as an attribute in their manifest,
    // which forces them to have a single application class.
    // in the future, we may want to let users do this or maybe specify a list of classes or
    // a package that will be searched for applications, to allow multiple applications in a single artifact.
    String mainClassName;
    try {
        Manifest manifest = BundleJarUtil.getManifest(artifactLocation);
        if (manifest == null) {
            return builder;
        }
        Attributes manifestAttributes = manifest.getMainAttributes();
        if (manifestAttributes == null) {
            return builder;
        }
        mainClassName = manifestAttributes.getValue(ManifestFields.MAIN_CLASS);
    } catch (ZipException e) {
        throw new InvalidArtifactException(String.format("Couldn't unzip artifact %s, please check it is a valid jar file.", artifactId), e);
    }
    if (mainClassName == null) {
        return builder;
    }
    try {
        Object appMain = artifactClassLoader.loadClass(mainClassName).newInstance();
        if (!(appMain instanceof Application)) {
            // possible for 3rd party plugin artifacts to have the main class set
            return builder;
        }
        Application app = (Application) appMain;
        java.lang.reflect.Type configType;
        // we can deserialize the config into that object. Otherwise it'll just be a Config
        try {
            configType = Artifacts.getConfigType(app.getClass());
        } catch (Exception e) {
            throw new InvalidArtifactException(String.format("Could not resolve config type for Application class %s in artifact %s. " + "The type must extend Config and cannot be parameterized.", mainClassName, artifactId));
        }
        Schema configSchema = configType == Config.class ? null : schemaGenerator.generate(configType);
        builder.addApp(new ApplicationClass(mainClassName, "", configSchema));
    } catch (ClassNotFoundException e) {
        throw new InvalidArtifactException(String.format("Could not find Application main class %s in artifact %s.", mainClassName, artifactId));
    } catch (UnsupportedTypeException e) {
        throw new InvalidArtifactException(String.format("Config for Application %s in artifact %s has an unsupported schema. " + "The type must extend Config and cannot be parameterized.", mainClassName, artifactId));
    } catch (InstantiationException | IllegalAccessException e) {
        throw new InvalidArtifactException(String.format("Could not instantiate Application class %s in artifact %s.", mainClassName, artifactId), e);
    }
    return builder;
}
Also used : Config(co.cask.cdap.api.Config) PluginConfig(co.cask.cdap.api.plugin.PluginConfig) Schema(co.cask.cdap.api.data.schema.Schema) Attributes(java.util.jar.Attributes) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) ZipException(java.util.zip.ZipException) Manifest(java.util.jar.Manifest) ZipException(java.util.zip.ZipException) EOFException(java.io.EOFException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) Application(co.cask.cdap.api.app.Application) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException)

Example 50 with ZipException

use of java.util.zip.ZipException in project MindsEye by SimiaCryptus.

the class SerializationTest method test.

@Nullable
@Override
public ToleranceStatistics test(@Nonnull final NotebookOutput log, @Nonnull final Layer layer, final Tensor... inputPrototype) {
    log.h1("Serialization");
    log.p("This apply will demonstrate the layer's JSON serialization, and verify deserialization integrity.");
    String prettyPrint = "";
    log.h2("Raw Json");
    try {
        prettyPrint = log.code(() -> {
            final JsonObject json = layer.getJson();
            @Nonnull final Layer echo = Layer.fromJson(json);
            if (echo == null)
                throw new AssertionError("Failed to deserialize");
            if (layer == echo)
                throw new AssertionError("Serialization did not copy");
            if (!layer.equals(echo))
                throw new AssertionError("Serialization not equal");
            echo.freeRef();
            return new GsonBuilder().setPrettyPrinting().create().toJson(json);
        });
        @Nonnull String filename = layer.getClass().getSimpleName() + "_" + log.getName() + ".json";
        log.p(log.file(prettyPrint, filename, String.format("Wrote Model to %s; %s characters", filename, prettyPrint.length())));
    } catch (RuntimeException e) {
        e.printStackTrace();
        Util.sleep(1000);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        Util.sleep(1000);
    }
    log.p("");
    @Nonnull Object outSync = new Object();
    if (prettyPrint.isEmpty() || prettyPrint.length() > 1024 * 64)
        Arrays.stream(SerialPrecision.values()).parallel().forEach(precision -> {
            try {
                @Nonnull File file = new File(log.getResourceDir(), log.getName() + "_" + precision.name() + ".zip");
                layer.writeZip(file, precision);
                @Nonnull final Layer echo = Layer.fromZip(new ZipFile(file));
                getModels().put(precision, echo);
                synchronized (outSync) {
                    log.h2(String.format("Zipfile %s", precision.name()));
                    log.p(log.link(file, String.format("Wrote Model apply %s precision to %s; %.3fMiB bytes", precision, file.getName(), file.length() * 1.0 / (0x100000))));
                }
                if (!isPersist())
                    file.delete();
                if (echo == null)
                    throw new AssertionError("Failed to deserialize");
                if (layer == echo)
                    throw new AssertionError("Serialization did not copy");
                if (!layer.equals(echo))
                    throw new AssertionError("Serialization not equal");
            } catch (RuntimeException e) {
                e.printStackTrace();
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            } catch (ZipException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    return null;
}
Also used : JsonObject(com.google.gson.JsonObject) Util(com.simiacryptus.util.Util) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZipException(java.util.zip.ZipException) Tensor(com.simiacryptus.mindseye.lang.Tensor) IOException(java.io.IOException) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) Charset(java.nio.charset.Charset) ToleranceStatistics(com.simiacryptus.mindseye.test.ToleranceStatistics) Layer(com.simiacryptus.mindseye.lang.Layer) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipFile(java.util.zip.ZipFile) SerialPrecision(com.simiacryptus.mindseye.lang.SerialPrecision) NotebookOutput(com.simiacryptus.util.io.NotebookOutput) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) GsonBuilder(com.google.gson.GsonBuilder) Nonnull(javax.annotation.Nonnull) JsonObject(com.google.gson.JsonObject) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) Layer(com.simiacryptus.mindseye.lang.Layer) ZipFile(java.util.zip.ZipFile) JsonObject(com.google.gson.JsonObject) File(java.io.File) ZipFile(java.util.zip.ZipFile) Nullable(javax.annotation.Nullable)

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