Example usage for java.util.zip ZipException printStackTrace

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

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static void upZipFile(File zipFile, String folderPath) {
    ZipFile zf;/*from www.j  a v a 2  s .com*/
    try {
        zf = new ZipFile(zipFile);
        for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = ((ZipEntry) entries.nextElement());
            if (entry.isDirectory()) {
                String dirstr = entry.getName();
                dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
                File f = new File(dirstr);
                f.mkdir();
                continue;
            }

            InputStream in = zf.getInputStream(entry);
            String str = folderPath + File.separator + entry.getName();
            str = new String(str.getBytes("8859_1"), "GB2312");
            File desFile = new File(str);
            if (!desFile.exists()) {
                File fileParentDir = desFile.getParentFile();
                if (!fileParentDir.exists()) {
                    fileParentDir.mkdirs();
                }
                desFile.createNewFile();
            }

            OutputStream out = new FileOutputStream(desFile);
            byte buffer[] = new byte[BUFF_SIZE];
            int realLength;
            while ((realLength = in.read(buffer)) > 0) {
                out.write(buffer, 0, realLength);
            }
            in.close();
            out.close();
        }
    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.mc.printer.model.utils.ZipHelper.java

/**
 * jar????/*from  w  w  w  .  j  ava2 s  .com*/
 *
 * @param jar ????jar
 * @param subDir jar???????
 * @param loc ????
 * @param force ????
 * @return
 */
public static boolean unZip(String jar, String subDir, String loc, boolean force) {
    try {
        File base = new File(loc);
        if (!base.exists()) {
            base.mkdirs();
        }

        ZipFile zip = new ZipFile(new File(jar));
        Enumeration<? extends ZipEntry> entrys = zip.entries();
        while (entrys.hasMoreElements()) {
            ZipEntry entry = entrys.nextElement();
            String name = entry.getName();
            if (!name.startsWith(subDir)) {
                continue;
            }
            //subDir
            name = name.replace(subDir, "").trim();
            if (name.length() < 2) {
                log.debug(name + "  < 2");
                continue;
            }
            if (entry.isDirectory()) {
                File dir = new File(base, name);
                if (!dir.exists()) {
                    dir.mkdirs();
                    log.debug("create directory");
                } else {
                    log.debug("the directory is existing.");
                }
                log.debug(name + " is a directory");
            } else {
                File file = new File(base, name);
                if (file.exists() && force) {
                    file.delete();
                }
                if (!file.exists()) {
                    InputStream in = zip.getInputStream(entry);
                    FileUtils.copyInputStreamToFile(in, file);
                    log.debug("create file.");
                    in.close();
                } else {
                    log.debug("the file is existing");
                }
                log.debug(name + " is not a directory");
            }
        }
        zip.close();
    } catch (ZipException ex) {
        ex.printStackTrace();
        log.error("file unzip failed.", ex);
        return false;
    } catch (IOException ex) {
        ex.printStackTrace();
        log.error("file IO failed", ex);
        return false;
    }
    return true;
}

From source file:de.lazyzero.kkMulticopterFlashTool.utils.Zip.java

public static void unzip2folder(File zipFile, File toFolder) throws FileExistsException {
    if (!toFolder.exists()) {
        toFolder.mkdirs();//from   w  w w .  j a  v a2s  .  c o m
    } else if (toFolder.isFile()) {
        throw new FileExistsException(toFolder.getName());
    }

    try {
        ZipEntry entry;
        @SuppressWarnings("resource")
        ZipFile zipfile = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> e = zipfile.entries();
        while (e.hasMoreElements()) {
            entry = e.nextElement();

            //            String newDir;
            //            if (entry.getName().indexOf("/") == -1) {
            //               newDir = zipFile.getName().substring(0, zipFile.getName().indexOf("."));
            //            } else {
            //               newDir = entry.getName().substring(0, entry.getName().indexOf("/"));
            //            }
            //            String folder = toFolder + (File.separatorChar+"") + newDir;
            //            System.out.println(folder);
            //            if ((new File(folder).mkdir())) {
            //               System.out.println("Done.");
            //            }
            System.out.println("Extracting: " + entry);
            if (entry.isDirectory()) {
                new File(toFolder + (File.separatorChar + "") + entry.getName()).mkdir();
            } else {
                BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry));
                int count;
                byte data[] = new byte[2048];
                String fileName = toFolder + (File.separatorChar + "") + entry.getName();
                FileOutputStream fos = new FileOutputStream(fileName);
                BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
                while ((count = is.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
                dest.close();
                is.close();
                System.out.println("extracted to: " + fileName);
            }

        }
    } catch (ZipException e1) {
        zipFile.delete();
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } finally {

    }

}

From source file:org.opf_labs.fmts.corpora.govdocs.GovDocs.java

/**
 * Get a new GovDocsCorpora instance from a root directory, can be in either zip
 * (1000 zip files) or directory (1,000 directories) based.
 * //ww w .ja  v a2 s  .  co m
 * @param root
 * @return the new GovDocsCorpora instance
 */
public static final GovDocsCorpora newInstance(final File root) {
    Preconditions.checkNotNull(root, "root==null");
    Preconditions.checkArgument(root.isDirectory(), "root should be an existing directory.");
    boolean isZip = isZip(root);
    Pattern foldPattern = (isZip) ? GovDocsZipped.ZIP_PATTERN : AbstractGovDocs.DIR_PATTERN;
    FilenameFilter filter = new RegexFileFilter(foldPattern);
    final File[] folders = root.listFiles(filter);
    int count = 0;
    long size = 0L;
    final Set<String> exts = new HashSet<String>();
    for (File folder : folders) {
        FolderDetails foldDets;
        try {
            foldDets = (isZip) ? getZipFolderDetails(folder) : getFolderDetails(folder);
        } catch (ZipException excep) {
            System.err.println(
                    "Folder: " + folder.getAbsolutePath() + " does not appear to be a valid zip file.");
            excep.printStackTrace();
            continue;
        } catch (IOException excep) {
            System.err.println("IOException reading Folder: " + folder.getAbsolutePath());
            excep.printStackTrace();
            continue;
        }
        count += foldDets.count;
        size += foldDets.size;
        exts.addAll(foldDets.exts);
    }
    CorpusDetails details = Corpora.details(count, size).name(GovDocsCorpora.NAME).type(GovDocsCorpora.TYPE)
            .extensions(exts).build();
    return (isZip) ? GovDocsZipped.newInstance(root, details, folders.length)
            : GovDocsDirectories.newInstance(root, details, folders.length);
}

From source file:org.kie.guvnor.m2repo.backend.server.GuvnorM2Repository.java

public static String loadPOMFromJar(InputStream jarInputStream) {
    try {//w  w  w  .  ja  va  2s  .c o m
        ZipInputStream zis = new ZipInputStream(jarInputStream);
        ZipEntry entry;

        while ((entry = zis.getNextEntry()) != null) {
            //System.out.println("entry: " + entry.getName() + ", " + entry.getSize());
            // consume all the data from this entry
            if (entry.getName().startsWith("META-INF/maven") && entry.getName().endsWith("pom.xml")) {
                InputStreamReader isr = new InputStreamReader(zis, "UTF-8");
                StringBuilder sb = new StringBuilder();
                for (int c = isr.read(); c != -1; c = isr.read()) {
                    sb.append((char) c);
                }
                return sb.toString();
            }
        }
    } catch (ZipException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:org.kie.guvnor.m2repo.backend.server.GuvnorM2Repository.java

public static String loadPOMFromJar(String jarPath) {
    try {/*w  ww  .  j a va2  s .c o m*/
        ZipFile zip = new ZipFile(new File(jarPath));

        for (Enumeration e = zip.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();

            if (entry.getName().startsWith("META-INF/maven") && entry.getName().endsWith("pom.xml")) {
                InputStream is = zip.getInputStream(entry);

                InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                StringBuilder sb = new StringBuilder();
                for (int c = isr.read(); c != -1; c = isr.read()) {
                    sb.append((char) c);
                }
                return sb.toString();
            }
        }
    } catch (ZipException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:com.github.wolfdogs.kemono.util.resource.zip.ZipFileResource.java

@Override
public InputStream getInputStream() {
    try {/*from   ww w  .ja v  a2  s.  c  o  m*/
        return zipFile.getInputStream(entry);
    } catch (ZipException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:fr.ippon.wip.config.dao.DeployConfigurationDecorator.java

/**
 * Retrieve all the configuration pasted in the deploy directory.
 * //from ww w.ja va  2  s. co  m
 * @return the configuration to deploy
 */
private synchronized void checkDeploy() {
    List<WIPConfiguration> deployedConfigurations = new ArrayList<WIPConfiguration>();
    for (File file : deployPath.listFiles(zipFilter)) {
        try {
            deployedConfigurations = unzip(new ZipFile(file));

        } catch (ZipException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    cleanDeployRepertory();
    for (WIPConfiguration newConfiguration : deployedConfigurations)
        super.create(newConfiguration);
}

From source file:eu.planets_project.services.utils.ZipUtils.java

/**
 * Lists all entries in this zip file. Each entry is a file/folder in this zip.
 * @param zip the zip file to scan//w w w  .  j  a  v  a2  s  .c o m
 * @return a list with all entry paths
 */
@SuppressWarnings("unchecked")
public static List<String> listZipEntries(File zip) {
    List<String> entryList = new ArrayList<String>();
    try {
        Zip64File zip64File = new Zip64File(zip);
        List<FileEntry> entries = zip64File.getListFileEntries();

        //         zip64File.close();

        if (entries.size() > 0) {
            for (FileEntry fileEntry : entries) {
                List<String> parents = getParents(fileEntry);
                if (parents != null) {
                    for (String currentPart : parents) {
                        if (!entryList.contains(currentPart)) {
                            entryList.add(currentPart);
                        }
                    }
                }
                entryList.add(fileEntry.getName());
            }
        } else {
            return new ArrayList<String>();
        }

    } catch (ZipException e) {
        log.severe("[ZipUtils] listZipEntries(): " + e.getMessage());
        //         e.printStackTrace();
        return entryList;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return entryList;
}

From source file:org.sead.nds.repository.util.ValidationJob.java

private String generateFileHash(String name, ZipFile zf) {

    ZipArchiveEntry archiveEntry1 = zf.getEntry(name);
    // Error check - add file sizes to compare against supplied stats

    long start = System.currentTimeMillis();
    InputStream inputStream = null;
    String realHash = null;//from   w w w  .ja  va 2s  .  c o m
    try {
        inputStream = zf.getInputStream(archiveEntry1);
        String hashtype = bagGenerator.getHashtype();
        if (hashtype != null) {
            if (hashtype.equals("SHA1 Hash")) {
                realHash = DigestUtils.sha1Hex(inputStream);
            } else if (hashtype.equals("SHA512 Hash")) {
                realHash = DigestUtils.sha512Hex(inputStream);
            }
        }

    } catch (ZipException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    log.debug("Retrieve/compute time = " + (System.currentTimeMillis() - start) + " ms");
    // Error check - add file sizes to compare against supplied stats
    bagGenerator.incrementTotalDataSize(archiveEntry1.getSize());
    return realHash;
}