Search in sources :

Example 26 with ZipException

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

the class SwfTranscoder method readSource.

// in will be closed
protected void readSource(InputStream in, long inputLength) throws IOException {
    final int uncompressedBodyLength;
    final boolean compressed;
    byte[] data;
    try {
        int n = in.read(partialHeader);
        assert n == PARTIAL_HEADER_LENGTH;
        uncompressedBodyLength = (partialHeader[4] & 0xFF | (partialHeader[5] & 0xFF) << 8 | (partialHeader[6] & 0xFF) << 16 | partialHeader[7] << 24) - PARTIAL_HEADER_LENGTH;
        compressed = partialHeader[0] == 0x43;
        data = FileUtil.loadBytes(in, compressed ? (int) inputLength - PARTIAL_HEADER_LENGTH : uncompressedBodyLength);
    } finally {
        in.close();
    }
    if (compressed) {
        final Inflater inflater = INFLATER.get();
        try {
            inflater.setInput(data);
            byte[] uncompressedData = new byte[uncompressedBodyLength];
            try {
                inflater.inflate(uncompressedData);
            } catch (DataFormatException e) {
                throw new ZipException(e.getMessage() != null ? e.getMessage() : "Invalid ZLIB data format");
            }
            data = uncompressedData;
        } finally {
            inflater.reset();
        }
    }
    buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
    readFrameSizeFrameRateAndFrameCount(data[0]);
}
Also used : DataFormatException(java.util.zip.DataFormatException) Inflater(java.util.zip.Inflater) ZipException(java.util.zip.ZipException)

Example 27 with ZipException

use of java.util.zip.ZipException in project bnd by bndtools.

the class bnd method getJar.

/**
	 * Central routine to get a JAR with error checking
	 * 
	 * @param s
	 */
Jar getJar(String s) {
    File f = getFile(s);
    if (f.isFile()) {
        try {
            return new Jar(f);
        } catch (ZipException e) {
            exception(e, "Not a jar/zip file: %s", f);
        } catch (Exception e) {
            exception(e, "Opening file: %s", f);
        }
        return null;
    }
    try {
        URL url = new URL(s);
        return new Jar(s, url.openStream());
    } catch (Exception e) {
    // Ignore
    }
    error("Not a file or proper url: %s", f);
    return null;
}
Also used : Jar(aQute.bnd.osgi.Jar) ZipException(java.util.zip.ZipException) File(java.io.File) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ZipException(java.util.zip.ZipException) URL(java.net.URL)

Example 28 with ZipException

use of java.util.zip.ZipException in project weex-example by KalicyZhou.

the class WXSoInstallMgrSdk method unZipSelectedFiles.

static boolean unZipSelectedFiles(String libName, int version, IWXUserTrackAdapter utAdapter) throws ZipException, IOException {
    String sourcePath = "lib/armeabi/lib" + libName + ".so";
    String zipPath = "";
    Context context = mContext;
    if (context == null) {
        return false;
    }
    ApplicationInfo aInfo = context.getApplicationInfo();
    if (null != aInfo) {
        zipPath = aInfo.sourceDir;
    }
    ZipFile zf;
    zf = new ZipFile(zipPath);
    try {
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            if (entry.getName().startsWith(sourcePath)) {
                InputStream in = null;
                FileOutputStream os = null;
                FileChannel channel = null;
                int total = 0;
                try {
                    //Make sure the old library is deleted.
                    removeSoIfExit(libName, version);
                    //Copy file
                    in = zf.getInputStream(entry);
                    os = context.openFileOutput("lib" + libName + "bk" + version + ".so", Context.MODE_PRIVATE);
                    channel = os.getChannel();
                    byte[] buffers = new byte[1024];
                    int realLength;
                    while ((realLength = in.read(buffers)) > 0) {
                        //os.write(buffers);
                        channel.write(ByteBuffer.wrap(buffers, 0, realLength));
                        total += realLength;
                    }
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (channel != null) {
                        try {
                            channel.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (os != null) {
                        try {
                            os.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if (zf != null) {
                        zf.close();
                        zf = null;
                    }
                }
                if (total > 0) {
                    return _loadUnzipSo(libName, version, utAdapter);
                } else {
                    return false;
                }
            }
        }
    } catch (java.io.IOException e) {
        e.printStackTrace();
    } finally {
        if (zf != null) {
            zf.close();
            zf = null;
        }
    }
    return false;
}
Also used : Context(android.content.Context) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) ZipEntry(java.util.zip.ZipEntry) ApplicationInfo(android.content.pm.ApplicationInfo) IOException(java.io.IOException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) FileOutputStream(java.io.FileOutputStream)

Example 29 with ZipException

use of java.util.zip.ZipException in project bytecode-viewer by Konloch.

the class ProcyonDecompiler method doSaveJarDecompiled.

/**
     * @author DeathMarine
     */
private void doSaveJarDecompiled(File inFile, File outFile) throws Exception {
    try (JarFile jfile = new JarFile(inFile);
        FileOutputStream dest = new FileOutputStream(outFile);
        BufferedOutputStream buffDest = new BufferedOutputStream(dest);
        ZipOutputStream out = new ZipOutputStream(buffDest)) {
        byte[] data = new byte[1024];
        DecompilerSettings settings = getDecompilerSettings();
        MetadataSystem metadataSystem = new MetadataSystem(new JarTypeLoader(jfile));
        DecompilationOptions decompilationOptions = new DecompilationOptions();
        decompilationOptions.setSettings(settings);
        decompilationOptions.setFullDecompilation(true);
        Enumeration<JarEntry> ent = jfile.entries();
        Set<JarEntry> history = new HashSet<JarEntry>();
        while (ent.hasMoreElements()) {
            JarEntry entry = ent.nextElement();
            if (entry.getName().endsWith(".class")) {
                JarEntry etn = new JarEntry(entry.getName().replace(".class", ".java"));
                if (history.add(etn)) {
                    out.putNextEntry(etn);
                    try {
                        String internalName = StringUtilities.removeRight(entry.getName(), ".class");
                        TypeReference type = metadataSystem.lookupType(internalName);
                        TypeDefinition resolvedType = null;
                        if ((type == null) || ((resolvedType = type.resolve()) == null)) {
                            throw new Exception("Unable to resolve type.");
                        }
                        Writer writer = new OutputStreamWriter(out);
                        settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(writer), decompilationOptions);
                        writer.flush();
                    } finally {
                        out.closeEntry();
                    }
                }
            } else {
                try {
                    JarEntry etn = new JarEntry(entry.getName());
                    if (history.add(etn))
                        continue;
                    history.add(etn);
                    out.putNextEntry(etn);
                    try {
                        InputStream in = jfile.getInputStream(entry);
                        if (in != null) {
                            try {
                                int count;
                                while ((count = in.read(data, 0, 1024)) != -1) {
                                    out.write(data, 0, count);
                                }
                            } finally {
                                in.close();
                            }
                        }
                    } finally {
                        out.closeEntry();
                    }
                } catch (ZipException ze) {
                    // it
                    if (!ze.getMessage().contains("duplicate")) {
                        throw ze;
                    }
                }
            }
        }
    }
}
Also used : DecompilationOptions(com.strobel.decompiler.DecompilationOptions) PlainTextOutput(com.strobel.decompiler.PlainTextOutput) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ZipException(java.util.zip.ZipException) ZipOutputStream(java.util.zip.ZipOutputStream) DecompilerSettings(com.strobel.decompiler.DecompilerSettings) HashSet(java.util.HashSet)

Example 30 with ZipException

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

the class Buffer method isZLIBSupportsLevelSwitchAP8083.

/**
 * See {@link #ZLIB_SUPPORTS_LEVEL_SWITCH_AP8083} for details.
 * @return hopefully mostly true but false in case we are on a broken zlib
 */
private static boolean isZLIBSupportsLevelSwitchAP8083() {
    ByteArrayOutputStream byteArrayOut;
    byte[] nullBytes = new byte[1024 * 1024];
    try (ZipOutputStream zipOut = new ZipOutputStream(byteArrayOut = new ByteArrayOutputStream())) {
        zipOut.setLevel(Deflater.BEST_SPEED);
        zipOut.putNextEntry(new ZipEntry("deflated.bin"));
        zipOut.write(nullBytes);
        zipOut.closeEntry();
        zipOut.putNextEntry(new ZipEntry("stored.bin"));
        zipOut.setLevel(Deflater.BEST_COMPRESSION);
        zipOut.write(nullBytes);
        zipOut.closeEntry();
    } catch (IOException e) {
        LOGGER.error("Unexpected error creating test zipped output", e);
        return false;
    }
    try (ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(byteArrayOut.toByteArray()))) {
        for (int i = 0; i < 2; i++) {
            zipIn.getNextEntry();
            while (zipIn.read(nullBytes) >= 0) {
            }
        }
    } catch (ZipException e) {
        return false;
    } catch (IOException e) {
        LOGGER.error("Unexpected error creating test zipped output", e);
        return false;
    }
    return true;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

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