Example usage for java.util.zip ZipException getMessage

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

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.gelakinetic.mtgfam.helpers.ZipUtils.java

/**
 * This method exports any data in this application's getFilesDir() into a zip file on external storage
 *
 * @param activity The application activity, for getting files and the like
 */// w ww .ja va 2s.  co  m
public static void exportData(Activity activity) {

    /* Make sure external storage exists */
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        ToastWrapper.makeAndShowText(activity, R.string.card_view_no_external_storage,
                ToastWrapper.LENGTH_LONG);
        return;
    }

    /* Check if permission is granted */
    if (ContextCompat.checkSelfPermission(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        /* Request the permission */
        ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
                FamiliarActivity.REQUEST_WRITE_EXTERNAL_STORAGE_BACKUP);
        return;
    }

    assert activity.getFilesDir() != null;

    String sharedPrefsDir = activity.getFilesDir().getPath();
    sharedPrefsDir = sharedPrefsDir.substring(0, sharedPrefsDir.lastIndexOf("/")) + "/shared_prefs/";

    ArrayList<File> files = findAllFiles(activity.getFilesDir(), new File(sharedPrefsDir));

    File sdCard = Environment.getExternalStorageDirectory();
    File zipOut = new File(sdCard, BACKUP_FILE_NAME);
    if (zipOut.exists()) {
        if (!zipOut.delete()) {
            return;
        }
    }
    try {
        zipIt(zipOut, files, activity);
        ToastWrapper.makeAndShowText(activity,
                activity.getString(R.string.main_export_success) + " " + zipOut.getAbsolutePath(),
                ToastWrapper.LENGTH_SHORT);
    } catch (ZipException e) {
        if (e.getMessage().equals("No entries")) {
            ToastWrapper.makeAndShowText(activity, R.string.main_export_no_data, ToastWrapper.LENGTH_SHORT);
        } else {
            ToastWrapper.makeAndShowText(activity, R.string.main_export_fail, ToastWrapper.LENGTH_SHORT);
        }
    } catch (IOException e) {
        ToastWrapper.makeAndShowText(activity, R.string.main_export_fail, ToastWrapper.LENGTH_SHORT);
    }
}

From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java

public static List<String> getAllClasses(ClassLoader cl, String packageName) {
    packageName = packageName.replace('.', '/');

    List<String> classes = new ArrayList<>();

    URL[] urls = ((URLClassLoader) cl).getURLs();
    for (URL url : urls) {
        //System.out.println(url.getFile());
        File jar = new File(url.getFile());

        if (jar.isDirectory()) {
            File subdir = new File(jar, packageName);
            if (!subdir.exists())
                continue;
            File[] files = subdir.listFiles();
            for (File file : files) {
                if (!file.isFile())
                    continue;
                if (file.getName().endsWith(".class"))
                    classes.add(file.getName().substring(0, file.getName().length() - 6).replace('/', '.'));
            }/* ww  w  .  j  a  v  a 2  s .c  o m*/
        }

        else {
            // try to open as ZIP
            try {
                ZipFile zip = new ZipFile(jar);
                for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
                    ZipEntry entry = entries.nextElement();
                    String name = entry.getName();
                    if (!name.startsWith(packageName))
                        continue;
                    if (name.endsWith(".class") && name.indexOf('$') < 0)
                        classes.add(name.substring(0, name.length() - 6).replace('/', '.'));
                }
                zip.close();
            } catch (ZipException e) {
                //System.out.println("Not a ZIP: " + e.getMessage());
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
        }
    }

    return classes;
}

From source file:de.baumann.thema.RequestActivity.java

private static void zipFile(final String path, final ZipOutputStream out, final String relPath)
        throws IOException {
    final File file = new File(path);
    if (!file.exists()) {
        if (DEBUG)
            Log.d(TAG, file.getName() + " does NOT exist!");
        return;/*  w w w .j  ava2 s .  c  o  m*/
    }
    final byte[] buf = new byte[1024];
    final String[] files = file.list();
    if (file.isFile()) {

        try (FileInputStream in = new FileInputStream(file.getAbsolutePath())) {
            out.putNextEntry(new ZipEntry(relPath + file.getName()));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        } catch (ZipException zipE) {
            if (DEBUG)
                Log.d(TAG, zipE.getMessage());
        } finally {
            if (out != null)
                out.closeEntry();

        }
    } else if (files.length > 0) // non-empty folder
    {
        for (String file1 : files) {
            zipFile(path + "/" + file1, out, relPath + file.getName() + "/");
        }
    }
}

From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java

/**
 * retrieves an entry from the package file (ZIP file)
 *
 * @param packageFile the sound package file
 * @param entryName the name of the entry in the ZIP file
 * @throws com.dreikraft.infactory.sound.SoundPackageException encapsulates
 * all low level (IO) exceptions//from  w ww . j  a  va 2  s  . c  o  m
 * @return the entry data as stream
 */
public static InputStream getPackageEntryStream(File packageFile, String entryName)
        throws SoundPackageException {
    if (packageFile == null) {
        throw new SoundPackageException(new IllegalArgumentException("missing package file"));
    }

    InputStream keyIn = null;
    try {
        ZipFile packageZip = new ZipFile(packageFile);

        // get key from package
        ZipEntry keyEntry = packageZip.getEntry(LICENSE_KEY_ENTRY);
        keyIn = packageZip.getInputStream(keyEntry);
        Key key = CryptoUtil.unwrapKey(keyIn, PUBLIC_KEY_FILE);

        // read entry
        ZipEntry entry = packageZip.getEntry(entryName);
        return new ZipClosingInputStream(packageZip,
                CryptoUtil.decryptInput(packageZip.getInputStream(entry), key));
    } catch (ZipException ex) {
        throw new SoundPackageException(ex);
    } catch (IOException ex) {
        throw new SoundPackageException(ex);
    } catch (CryptoException ex) {
        throw new SoundPackageException(ex);
    } finally {
        try {
            if (keyIn != null) {
                keyIn.close();
            }
        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
        }
    }
}

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

private static String loadPomFromJar(final File file) {
    InputStream is = null;//  www.  j  a va2  s .  com
    InputStreamReader isr = null;
    try {
        ZipFile zip = new ZipFile(file);

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

            if (entry.getName().startsWith("META-INF/maven") && entry.getName().endsWith("pom.xml")) {
                is = zip.getInputStream(entry);
                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) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } finally {
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException e) {
            }
        }
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }

    return null;
}

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//from w ww  .j  a  v  a  2s  . c  om
 * @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.bonitasoft.web.designer.controller.utils.Unzipper.java

public Path unzipInTempDir(InputStream is, String tempDirPrefix) throws IOException {
    Path tempDirectory = Files.createTempDirectory(temporaryZipPath, tempDirPrefix);
    Path zipFile = writeInDir(is, tempDirectory);
    try {/* w w w.  j a va 2  s.  c o  m*/
        ZipUtil.unpack(zipFile.toFile(), tempDirectory.toFile());
    } catch (org.zeroturnaround.zip.ZipException e) {
        throw new ZipException(e.getMessage());
    } finally {
        FileUtils.deleteQuietly(zipFile.toFile());
    }
    return tempDirectory;
}

From source file:asciidoc.maven.plugin.tools.ZipHelper.java

public File unzipEntry(File _zipFile, String _name) {
    File directory = null;/*  w ww  .  j  a va2  s.  com*/
    try {
        if (this.log.isDebugEnabled())
            this.log.debug("zip file: " + _zipFile.getAbsolutePath());
        ZipEntry zipEntry = null;
        String zipEntryName = null;
        ZipFile jarZipFile = new ZipFile(_zipFile);
        Enumeration<? extends ZipEntry> e = jarZipFile.entries();
        while (e.hasMoreElements()) {
            zipEntry = (ZipEntry) e.nextElement();
            zipEntryName = zipEntry.getName();
            if (zipEntryName.startsWith(_name) && zipEntryName.endsWith(".zip")) {
                if (this.log.isInfoEnabled())
                    this.log.info("Found in " + zipEntryName);
                directory = new File(_zipFile.getParent(), FilenameUtils.removeExtension(zipEntryName));
                break;
            }
        }
        if (directory != null && !directory.exists()) {
            unzipEntry(jarZipFile, zipEntry, _zipFile.getParentFile());
            File asciiDocArchive = new File(_zipFile.getParent(), zipEntryName);
            unzipArchive(asciiDocArchive, _zipFile.getParentFile());
            asciiDocArchive.deleteOnExit();
        }

    } catch (ZipException ze) {
        this.log.error(ze.getMessage(), ze);
    } catch (IOException ioe) {
        this.log.error(ioe.getMessage(), ioe);
    }
    return directory;
}

From source file:cn.vlabs.clb.server.utils.UnCompress.java

public void unzip(File file) {
    ZipFile zipfile = null;//from   w  w w  .j  a  v  a2  s . co m
    try {
        if (encoding == null)
            zipfile = new ZipFile(file);
        else
            zipfile = new ZipFile(file, encoding);
        Enumeration<ZipArchiveEntry> iter = zipfile.getEntries();
        while (iter.hasMoreElements()) {
            ZipArchiveEntry entry = iter.nextElement();
            if (!entry.isDirectory()) {
                InputStream entryis = null;
                try {
                    entryis = zipfile.getInputStream(entry);
                    listener.onNewEntry(new ZipEntryAdapter(entry, zipfile));
                } finally {
                    IOUtils.closeQuietly(entryis);
                }
            }
        }
    } catch (ZipException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error("file not found " + e.getMessage(), e);
    } finally {
        if (zipfile != null) {
            try {
                zipfile.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }
}

From source file:de.mpg.imeji.logic.export.format.ZIPExport.java

/**
 * This method exports all images of the current browse page as a zip file
 * //  w  w w. ja v  a 2 s. com
 * @throws Exception
 * @throws URISyntaxException
 */
public void exportAllImages(SearchResult sr, OutputStream out) throws URISyntaxException, Exception {
    List<String> source = sr.getResults();
    ZipOutputStream zip = new ZipOutputStream(out);
    try {
        // Create the ZIP file
        for (int i = 0; i < source.size(); i++) {
            SessionBean session = (SessionBean) BeanHelper.getSessionBean(SessionBean.class);
            ItemController ic = new ItemController(session.getUser());
            Item item = ic.retrieve(new URI(source.get(i)));
            StorageController sc = new StorageController();
            try {
                zip.putNextEntry(new ZipEntry(item.getFilename()));
                sc.read(item.getFullImageUrl().toString(), zip, false);
                // Complete the entry
                zip.closeEntry();
            } catch (ZipException ze) {
                if (ze.getMessage().contains("duplicate entry")) {
                    String name = i + "_" + item.getFilename();
                    zip.putNextEntry(new ZipEntry(name));
                    sc.read(item.getFullImageUrl().toString(), zip, false);
                    // Complete the entry
                    zip.closeEntry();
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        // Complete the ZIP file
        zip.close();
    }
}