Search in sources :

Example 81 with ZipException

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

the class ToStringUtils method decompressFromBase64.

/**
 * Get the original string from its compressed base64 representation.
 * @param base64CompressedString Compressed Base64 representation of the string.
 * @return The original string.
 * @throws NullPointerException If base64CompressedString is null.
 * @throws IllegalArgumentException If base64CompressedString is not null, but has a length of zero or if the input is invalid.
 */
@SneakyThrows(IOException.class)
public static String decompressFromBase64(final String base64CompressedString) {
    Exceptions.checkNotNullOrEmpty(base64CompressedString, "base64CompressedString");
    try {
        @Cleanup final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64CompressedString.getBytes(UTF_8));
        @Cleanup final InputStream base64InputStream = Base64.getDecoder().wrap(byteArrayInputStream);
        @Cleanup final GZIPInputStream gzipInputStream = new GZIPInputStream(base64InputStream);
        return IOUtils.toString(gzipInputStream, UTF_8);
    } catch (ZipException | EOFException e) {
        // exceptions thrown for invalid encoding and partial data.
        throw new IllegalArgumentException("Invalid base64 input.", e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EOFException(java.io.EOFException) ZipException(java.util.zip.ZipException) Cleanup(lombok.Cleanup) SneakyThrows(lombok.SneakyThrows)

Example 82 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)

Example 83 with ZipException

use of java.util.zip.ZipException in project hale by halestudio.

the class ReflectionHelper method getFilesFromPackage.

/**
 * Returns an array of all files contained by a given package
 *
 * @param pkg the package (e.g. "de.igd.fhg.CityServer3D")
 * @return an array of files
 * @throws IOException if the package could not be found
 */
public static synchronized File[] getFilesFromPackage(String pkg) throws IOException {
    File[] files;
    JarFile jarFile = null;
    try {
        URL u = _packageResolver.resolve(pkg);
        if (u != null && !u.toString().startsWith("jar:")) {
            // $NON-NLS-1$
            // we got the package as an URL. Simply create a file
            // from this URL
            File dir;
            try {
                dir = new File(u.toURI());
            } catch (URISyntaxException e) {
                // if the URL contains spaces and they have not been
                // replaced by %20 then we'll have to use the following line
                dir = new File(u.getFile());
            }
            if (!dir.isDirectory()) {
                // try another method
                dir = new File(u.getFile());
            }
            files = null;
            if (dir.isDirectory()) {
                files = dir.listFiles();
            }
        } else {
            // get the current jar file and search it
            if (u != null && u.toString().startsWith("jar:file:")) {
                // first try using URL and File
                try {
                    String p = u.toString().substring(4);
                    // $NON-NLS-1$
                    p = p.substring(0, p.indexOf("!/"));
                    File file = new File(URI.create(p));
                    p = file.getAbsolutePath();
                    try {
                        jarFile = new JarFile(p);
                    } catch (ZipException e) {
                        // $NON-NLS-1$
                        throw new IllegalArgumentException("No zip file: " + p, e);
                    }
                } catch (Throwable e1) {
                    // second try directly using path
                    String p = u.toString().substring(9);
                    // $NON-NLS-1$
                    p = p.substring(0, p.indexOf("!/"));
                    try {
                        jarFile = new JarFile(p);
                    } catch (ZipException e2) {
                        // $NON-NLS-1$
                        throw new IllegalArgumentException("No zip file: " + p, e2);
                    }
                }
            } else {
                u = getCurrentJarURL();
                // open jar file
                JarURLConnection juc = (JarURLConnection) u.openConnection();
                jarFile = juc.getJarFile();
            }
            // enumerate entries and add those that match the package path
            Enumeration<JarEntry> entries = jarFile.entries();
            ArrayList<String> file_names = new ArrayList<String>();
            // $NON-NLS-1$ //$NON-NLS-2$
            String package_path = pkg.replaceAll("\\.", "/");
            boolean slashed = false;
            if (package_path.charAt(0) == '/') {
                package_path = package_path.substring(1);
                slashed = true;
            }
            while (entries.hasMoreElements()) {
                JarEntry j = entries.nextElement();
                if (j.getName().matches("^" + package_path + ".+\\..+")) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    if (slashed) {
                        // $NON-NLS-1$
                        file_names.add("/" + j.getName());
                    } else {
                        file_names.add(j.getName());
                    }
                }
            }
            // convert list to array
            files = new File[file_names.size()];
            Iterator<String> i = file_names.iterator();
            int n = 0;
            while (i.hasNext()) {
                files[n++] = new File(i.next());
            }
        }
    } catch (Throwable e) {
        // $NON-NLS-1$
        throw new IOException("Could not find package: " + pkg, e);
    } finally {
        if (jarFile != null) {
            jarFile.close();
        }
    }
    if (files != null && files.length == 0)
        // let's not require paranoid callers
        return null;
    return files;
}
Also used : JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) ZipException(java.util.zip.ZipException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 84 with ZipException

use of java.util.zip.ZipException in project alfresco-remote-api by Alfresco.

the class SiteExportServiceTest method getAcpEntries.

private List<String> getAcpEntries(InputStream inputStream) throws Exception {
    List<String> entries = Lists.newArrayList();
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry entry = null;
    try {
        while ((entry = zipInputStream.getNextEntry()) != null) {
            entries.add(entry.getName());
        }
    } catch (ZipException e) {
    // ignore
    }
    return entries;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException)

Example 85 with ZipException

use of java.util.zip.ZipException in project alfresco-remote-api by Alfresco.

the class CustomModelUploadPost method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    if (!customModelService.isModelAdmin(AuthenticationUtil.getFullyAuthenticatedUser())) {
        throw new WebScriptException(Status.STATUS_FORBIDDEN, PermissionDeniedException.DEFAULT_MESSAGE_ID);
    }
    FormData formData = (FormData) req.parseContent();
    if (formData == null || !formData.getIsMultiPart()) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_not_multi_part_req");
    }
    ImportResult resultData = null;
    boolean processed = false;
    for (FormData.FormField field : formData.getFields()) {
        if (field.getIsFile()) {
            final String fileName = field.getFilename();
            File tempFile = createTempFile(field.getInputStream());
            try (ZipFile zipFile = new ZipFile(tempFile, StandardCharsets.UTF_8)) {
                resultData = processUpload(zipFile, field.getFilename());
            } catch (ZipException ze) {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_not_zip_format", new Object[] { fileName });
            } catch (IOException io) {
                throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "cmm.rest_api.model.import_process_zip_file_failure", io);
            } finally {
                // now the import is done, delete the temp file
                tempFile.delete();
            }
            processed = true;
            break;
        }
    }
    if (!processed) {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_no_zip_file_uploaded");
    }
    // If we get here, then importing the custom model didn't throw any exceptions.
    Map<String, Object> model = new HashMap<>(2);
    model.put("importedModelName", resultData.getImportedModelName());
    model.put("shareExtXMLFragment", resultData.getShareExtXMLFragment());
    return model;
}
Also used : FormData(org.springframework.extensions.webscripts.servlet.FormData) HashMap(java.util.HashMap) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

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