Example usage for java.util.zip ZipException ZipException

List of usage examples for java.util.zip ZipException ZipException

Introduction

In this page you can find the example usage for java.util.zip ZipException ZipException.

Prototype


public ZipException(String s) 

Source Link

Document

Constructs a ZipException with the specified detail message.

Usage

From source file:br.univali.celine.lms.utils.zip.Zip.java

public void unzip(File zipFile, File dir) throws Exception {

    InputStream is = null;//w ww . j  a  v  a2  s  . c om
    OutputStream os = null;
    ZipFile zip = null;

    try {

        zip = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> e = zip.entries();

        byte[] buffer = new byte[2048];
        int currentEntry = 0;

        while (e.hasMoreElements()) {

            ZipEntry zipEntry = (ZipEntry) e.nextElement();
            File file = new File(dir, zipEntry.getName());
            currentEntry++;

            if (zipEntry.isDirectory()) {

                if (!file.exists())
                    file.mkdirs();

                continue;

            }

            if (!file.getParentFile().exists())
                file.getParentFile().mkdirs();

            try {

                int bytesLidos = 0;

                is = zip.getInputStream(zipEntry);
                os = new FileOutputStream(file);

                if (is == null)
                    throw new ZipException("Erro ao ler a entrada do zip: " + zipEntry.getName());

                while ((bytesLidos = is.read(buffer)) > 0)
                    os.write(buffer, 0, bytesLidos);

                if (listener != null)
                    listener.unzip((100 * currentEntry) / zip.size());

            } finally {

                if (is != null)
                    try {
                        is.close();
                    } catch (Exception ex) {
                    }

                if (os != null)
                    try {
                        os.close();
                    } catch (Exception ex) {
                    }

            }
        }

    } finally {

        if (zip != null)
            try {
                zip.close();
            } catch (Exception e) {
            }

    }

}

From source file:gui.accessories.DownloadProgressWork.java

/**
 * Uncompress all the files contained in the compressed file and its folders in the same folder where the zip file is placed. Doesn't respects the
 * directory tree of the zip file. This method seems a clear candidate to ZipManager.
 *
 * @param file Zip file./*from   w  w  w  . jav a  2 s  . c  o m*/
 * @return Count of files uncopressed.
 * @throws ZipException Exception.
 */
private int doUncompressZip(File file) throws ZipException {
    int fileCount = 0;
    try {
        byte[] buf = new byte[1024];
        ZipFile zipFile = new ZipFile(file);

        Enumeration zipFileEntries = zipFile.entries();

        while (zipFileEntries.hasMoreElements()) {
            ZipEntry zipentry = (ZipEntry) zipFileEntries.nextElement();
            if (zipentry.isDirectory()) {
                continue;
            }
            File entryZipFile = new File(zipentry.getName());
            File outputFile = new File(file.getParentFile(), entryZipFile.getName());
            FileOutputStream fileoutputstream = new FileOutputStream(outputFile);
            InputStream is = zipFile.getInputStream(zipentry);
            int n;
            while ((n = is.read(buf, 0, 1024)) > -1) {
                fileoutputstream.write(buf, 0, n);
            }
            fileoutputstream.close();
            is.close();
            fileoutputstream.close();
            fileCount++;
        }
        zipFile.close();

    } catch (IOException ex) {
        throw new ZipException(ex.getMessage());
    }

    return fileCount;
}

From source file:com.proofpoint.jaxrs.AbstractMapperTest.java

@Test(expectedExceptions = IOException.class)
public void testOtherIOExceptionThrowsIOException() throws IOException {
    try {/*w ww.  ja  va 2  s.c o  m*/
        mapper.readFrom(Object.class, Object.class, null, null, null, new InputStream() {
            @Override
            public int read() throws IOException {
                throw new ZipException("forced ZipException");
            }

            @Override
            public int read(byte[] b) throws IOException {
                throw new ZipException("forced ZipException");
            }

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                throw new ZipException("forced ZipException");
            }
        });
        fail("Should have thrown an IOException");
    } catch (WebApplicationException e) {
        fail("Should not have received an IOException", e);
    }
}

From source file:io.airlift.jaxrs.TestJsonMapper.java

@Test(expectedExceptions = IOException.class)
public void testOtherIOExceptionThrowsIOException() throws IOException {
    try {//w  w w .  j  a v  a2 s. co  m
        JsonMapper jsonMapper = new JsonMapper(new ObjectMapper());
        jsonMapper.readFrom(Object.class, Object.class, null, null, null, new InputStream() {
            @Override
            public int read() throws IOException {
                throw new ZipException("forced ZipException");
            }

            @Override
            public int read(byte[] b) throws IOException {
                throw new ZipException("forced ZipException");
            }

            @Override
            public int read(byte[] b, int off, int len) throws IOException {
                throw new ZipException("forced ZipException");
            }
        });
        Assert.fail("Should have thrown an IOException");
    } catch (WebApplicationException e) {
        Assert.fail("Should not have received a WebApplicationException", e);
    }
}

From source file:mobac.mapsources.loader.MapPackManager.java

public int getMapPackRevision(File mapPackFile) throws ZipException, IOException {
    ZipFile zip = new ZipFile(mapPackFile);
    try {//from  ww w  .  ja  va  2 s.com
        ZipEntry entry = zip.getEntry("META-INF/MANIFEST.MF");
        if (entry == null)
            throw new ZipException("Unable to find MANIFEST.MF");
        Manifest mf = new Manifest(zip.getInputStream(entry));
        Attributes a = mf.getMainAttributes();
        String mpv = a.getValue("MapPackRevision").trim();
        return Utilities.parseSVNRevision(mpv);
    } catch (NumberFormatException e) {
        return -1;
    } finally {
        zip.close();
    }
}

From source file:com.taobao.android.builder.tools.classinject.CodeInjectByJavassist.java

private static JarFile newJarFile(File jar) throws IOException {
    try {/*from w ww.  j  a  v  a 2 s .c o m*/
        return new JarFile(jar);
    } catch (ZipException zex) {
        throw new ZipException("error in opening zip file " + jar);
    }
}

From source file:com.maxl.java.aips2sqlite.AllDown.java

private void unzipToTemp(File dst) {
    try {// w  w w  . jav a 2 s.c om
        ZipInputStream zin = new ZipInputStream(new FileInputStream(dst));
        String workingDir = "./downloads/tmp" + File.separator + "unzipped_tmp";
        byte buffer[] = new byte[4096];
        int bytesRead;

        ZipEntry entry = null;
        while ((entry = zin.getNextEntry()) != null) {
            String dirName = workingDir;

            int endIndex = entry.getName().lastIndexOf(File.separatorChar);
            if (endIndex != -1) {
                dirName += entry.getName().substring(0, endIndex);
            }

            File newDir = new File(dirName);
            // If the directory that this entry should be inflated under does not exist, create it
            if (!newDir.exists() && !newDir.mkdir()) {
                throw new ZipException("Could not create directory " + dirName + "\n");
            }

            // Copy data from ZipEntry to file
            FileOutputStream fos = new FileOutputStream(workingDir + File.separator + entry.getName());
            while ((bytesRead = zin.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
            fos.close();
        }
        zin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.technicpack.launchercore.util.ZipUtils.java

/**
 * Unzips a file into the specified directory.
 *
 * @param zip          file to unzip//from   w ww.  j a  va2 s.  com
 * @param output       directory to unzip into
 * @param extractRules extractRules for this zip file. May be null indicating no rules.
 * @param listener     to update progress on - may be null for no progress indicator
 */
public static void unzipFile(File zip, File output, ExtractRules extractRules, DownloadListener listener)
        throws IOException {
    if (!zip.exists()) {
        Utils.getLogger().log(Level.SEVERE, "File to unzip does not exist: " + zip.getAbsolutePath());
        return;
    }
    if (!output.exists()) {
        output.mkdirs();
    }

    ZipFile zipFile = new ZipFile(zip);
    int size = zipFile.size() + 1;
    int progress = 1;
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = null;

            try {
                entry = entries.nextElement();
            } catch (IllegalArgumentException ex) {
                //We must catch & rethrow as a zip exception because some crappy code in the zip lib will
                //throw illegal argument exceptions for malformed zips.
                throw new ZipException("IllegalArgumentException while parsing next element.");
            }

            if ((extractRules == null || extractRules.shouldExtract(entry.getName()))
                    && !entry.getName().contains("../")) {
                File outputFile = new File(output, entry.getName());

                if (outputFile.getParentFile() != null) {
                    outputFile.getParentFile().mkdirs();
                }

                if (!entry.isDirectory()) {
                    unzipEntry(zipFile, entry, outputFile);
                }
            }

            if (listener != null) {
                float totalProgress = (float) progress / (float) size;
                listener.stateChanged("Extracting " + entry.getName() + "...", totalProgress * 100.0f);
            }
            progress++;
        }
    } finally {
        zipFile.close();
    }
}

From source file:net.technicpack.utilslib.ZipUtils.java

public static void unzipFile(File zip, File output, IZipFileFilter fileFilter, DownloadListener listener)
        throws IOException, InterruptedException {
    if (!zip.exists()) {
        Utils.getLogger().log(Level.SEVERE, "File to unzip does not exist: " + zip.getAbsolutePath());
        return;//from  w ww.j  av  a  2  s  .  c  om
    }
    if (!output.exists()) {
        output.mkdirs();
    }

    ZipFile zipFile = new ZipFile(zip);
    int size = zipFile.size() + 1;
    int progress = 1;
    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            if (Thread.interrupted())
                throw new InterruptedException();

            ZipEntry entry = null;

            try {
                entry = entries.nextElement();
            } catch (IllegalArgumentException ex) {
                //We must catch & rethrow as a zip exception because some crappy code in the zip lib will
                //throw illegal argument exceptions for malformed zips.
                throw new ZipException("IllegalArgumentException while parsing next element.");
            }

            if (!entry.getName().contains("../")
                    && (fileFilter == null || fileFilter.shouldExtract(entry.getName()))) {
                File outputFile = new File(output, entry.getName());

                if (outputFile.getParentFile() != null) {
                    outputFile.getParentFile().mkdirs();
                }

                if (!entry.isDirectory()) {
                    unzipEntry(zipFile, entry, outputFile);
                }
            }

            if (listener != null) {
                float totalProgress = (float) progress / (float) size;
                listener.stateChanged("Extracting " + entry.getName() + "...", totalProgress * 100.0f);
            }
            progress++;
        }
    } finally {
        zipFile.close();
    }
}

From source file:org.apache.james.mailbox.backup.LongExtraField.java

@Override
public void parseFromLocalFileData(byte[] buffer, int offset, int length) throws ZipException {
    if (length != Long.BYTES) {
        throw new ZipException(
                "Unexpected data length for ExtraField. Expected " + Long.BYTES + " but got " + length + ".");
    }/*ww  w .  j  a  va 2s. c o m*/
    value = Optional.of(ByteBuffer.wrap(buffer, offset, Long.BYTES).order(ByteOrder.LITTLE_ENDIAN).getLong());
}