Search in sources :

Example 16 with ZipException

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

the class ZipEntryTest method testMaxLengthExtra_zip64.

public void testMaxLengthExtra_zip64() throws Exception {
    // Not quite the max length (65535), but large enough that there's no space
    // for the zip64 extended info header.
    byte[] maxLengthExtra = new byte[65530];
    File f = createTemporaryZipFile();
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f)), true);
    ZipEntry ze = new ZipEntry("x");
    ze.setExtra(maxLengthExtra);
    try {
        out.putNextEntry(ze);
        fail();
    } catch (ZipException expected) {
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) ZipFile(java.util.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 17 with ZipException

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

the class OldJarFileTest method test_getInputStreamLjava_util_jar_JarEntry.

public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException {
    Support_Resources.copyFile(resources, null, jarName);
    File localFile = new File(resources, jarName);
    byte[] b = new byte[1024];
    JarFile jf = new JarFile(localFile);
    InputStream is = jf.getInputStream(jf.getEntry(entryName));
    assertTrue("Returned invalid stream", is.available() > 0);
    int r = is.read(b, 0, 1024);
    is.close();
    StringBuilder stringBuffer = new StringBuilder(r);
    for (int i = 0; i < r; i++) {
        stringBuffer.append((char) (b[i] & 0xff));
    }
    String contents = stringBuffer.toString();
    assertTrue("Incorrect stream read", contents.indexOf("bar") > 0);
    jf.close();
    jf = new JarFile(localFile);
    InputStream in = jf.getInputStream(new JarEntry("invalid"));
    assertNull("Got stream for non-existent entry", in);
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        jf = new JarFile(signedFile);
        JarEntry jre = new JarEntry("foo/bar/A.class");
        jf.getInputStream(jre);
    // InputStream returned in any way, exception can be thrown in case
    // of reading from this stream only.
    // fail("Should throw ZipException");
    } catch (ZipException expected) {
    }
    try {
        Support_Resources.copyFile(resources, null, jarName);
        File signedFile = new File(resources, jarName);
        jf = new JarFile(signedFile);
        JarEntry jre = new JarEntry("foo/bar/A.class");
        jf.close();
        jf.getInputStream(jre);
        // InputStream returned in any way, exception can be thrown in case
        // of reading from this stream only.
        // The same for IOException
        fail("Should throw IllegalStateException");
    } catch (IllegalStateException expected) {
    }
}
Also used : InputStream(java.io.InputStream) ZipException(java.util.zip.ZipException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 18 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 19 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)

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

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