Example usage for org.apache.commons.compress.archivers.zip ZipFile close

List of usage examples for org.apache.commons.compress.archivers.zip ZipFile close

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipFile close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the archive.

Usage

From source file:adams.core.io.ZipUtils.java

/**
 * Unzips the files in a ZIP file. Files can be filtered based on their
 * filename, using a regular expression (the matching sense can be inverted).
 *
 * @param input   the ZIP file to unzip// w  w  w . j a v a2 s  .  c  o  m
 * @param outputDir   the directory where to store the extracted files
 * @param createDirs   whether to re-create the directory structure from the
 *          ZIP file
 * @param match   the regular expression that the files are matched against
 * @param invertMatch   whether to invert the matching sense
 * @param bufferSize   the buffer size to use
 * @param errors   for storing potential errors
 * @return      the successfully extracted files
 */
@MixedCopyright(copyright = "Apache compress commons", license = License.APACHE2, url = "http://commons.apache.org/compress/examples.html")
public static List<File> decompress(File input, File outputDir, boolean createDirs, BaseRegExp match,
        boolean invertMatch, int bufferSize, StringBuilder errors) {
    List<File> result;
    ZipFile archive;
    Enumeration<ZipArchiveEntry> enm;
    ZipArchiveEntry entry;
    File outFile;
    String outName;
    byte[] buffer;
    BufferedInputStream in;
    BufferedOutputStream out;
    FileOutputStream fos;
    int len;
    String error;
    long read;

    result = new ArrayList<>();
    archive = null;
    try {
        // unzip archive
        buffer = new byte[bufferSize];
        archive = new ZipFile(input.getAbsoluteFile());
        enm = archive.getEntries();
        while (enm.hasMoreElements()) {
            entry = enm.nextElement();

            if (entry.isDirectory() && !createDirs)
                continue;

            // does name match?
            if (!match.isMatchAll() && !match.isEmpty()) {
                if (invertMatch && match.isMatch(entry.getName()))
                    continue;
                else if (!invertMatch && !match.isMatch(entry.getName()))
                    continue;
            }

            // extract
            if (entry.isDirectory() && createDirs) {
                outFile = new File(outputDir.getAbsolutePath() + File.separator + entry.getName());
                if (!outFile.mkdirs()) {
                    error = "Failed to create directory '" + outFile.getAbsolutePath() + "'!";
                    System.err.println(error);
                    errors.append(error + "\n");
                }
            } else {
                in = null;
                out = null;
                fos = null;
                outName = null;
                try {
                    // assemble output name
                    outName = outputDir.getAbsolutePath() + File.separator;
                    if (createDirs)
                        outName += entry.getName();
                    else
                        outName += new File(entry.getName()).getName();

                    // create directory, if necessary
                    outFile = new File(outName).getParentFile();
                    if (!outFile.exists()) {
                        if (!outFile.mkdirs()) {
                            error = "Failed to create directory '" + outFile.getAbsolutePath() + "', "
                                    + "skipping extraction of '" + outName + "'!";
                            System.err.println(error);
                            errors.append(error + "\n");
                            continue;
                        }
                    }

                    // extract data
                    in = new BufferedInputStream(archive.getInputStream(entry));
                    fos = new FileOutputStream(outName);
                    out = new BufferedOutputStream(fos, bufferSize);
                    read = 0;
                    while (read < entry.getSize()) {
                        len = in.read(buffer);
                        read += len;
                        out.write(buffer, 0, len);
                    }
                    result.add(new File(outName));
                } catch (Exception e) {
                    error = "Error extracting '" + entry.getName() + "' to '" + outName + "': " + e;
                    System.err.println(error);
                    errors.append(error + "\n");
                } finally {
                    FileUtils.closeQuietly(in);
                    FileUtils.closeQuietly(out);
                    FileUtils.closeQuietly(fos);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        errors.append("Error occurred: " + e + "\n");
    } finally {
        if (archive != null) {
            try {
                archive.close();
            } catch (Exception e) {
                // ignored
            }
        }
    }

    return result;
}

From source file:com.naryx.tagfusion.expression.function.file.Unzip.java

private void performUnzip(cfSession _session, File _zipfile, File _destination, String charset,
        boolean _flatten, boolean overwrite) throws cfmRunTimeException {
    ZipFile zFile = null;
    try {/*w  ww  .j av  a2  s.c o m*/
        zFile = new ZipFile(_zipfile, charset);
    } catch (IOException ze) {
        throwException(_session, "Failed to extract zip file. Check the file is a valid zip file.");
    }

    BufferedInputStream in = null;
    InputStream zIn = null;
    FileOutputStream fout = null;
    File nextFile = null;
    String destinationFilename;
    byte[] buffer = new byte[4096];
    int read;

    try {
        Enumeration<? extends ZipArchiveEntry> files = zFile.getEntries();

        if (files == null) {
            throwException(_session, "Failed to extract zip file. Check the file is a valid zip file.");
        }

        ZipArchiveEntry nextEntry;

        // while unzip stuff goes here
        while (files.hasMoreElements()) {
            nextEntry = files.nextElement();
            destinationFilename = nextEntry.getName();
            File checkFile = new File(
                    _destination.getAbsolutePath() + File.separatorChar + destinationFilename);

            if (checkFile.exists() && !overwrite) {
                throwException(_session, "File already exist");

            } else {
                if (!nextEntry.isDirectory()) {

                    if (_flatten) {
                        int pathEnd = destinationFilename.lastIndexOf('/');
                        if (pathEnd != -1)
                            destinationFilename = destinationFilename.substring(pathEnd + 1);
                    }

                    nextFile = new File(
                            _destination.getAbsolutePath() + File.separatorChar + destinationFilename);
                    try {
                        nextFile = nextFile.getCanonicalFile();
                    } catch (IOException ignore) {
                    } // use original nextFile if getCanonicalFile() fails

                    File parent = nextFile.getParentFile();
                    if (parent != null) {
                        parent.mkdirs(); // create the parent directory structure if needed
                    }

                    try {
                        zIn = zFile.getInputStream(nextEntry);
                        in = new BufferedInputStream(zIn);
                        fout = new FileOutputStream(nextFile, false);
                        while ((read = in.read(buffer)) != -1) {
                            fout.write(buffer, 0, read);
                        }

                        fout.flush();
                    } catch (IOException ioe) {
                        throwException(_session, "Failed to extract entry [" + nextEntry.getName()
                                + "] from zip file to " + nextFile.getAbsolutePath()
                                + ". Check the permissions are suitable to allow this file to be written.");
                    } finally {
                        StreamUtil.closeStream(in);
                        StreamUtil.closeStream(zIn);
                        StreamUtil.closeStream(fout);
                    }

                } else if (!_flatten) {
                    destinationFilename = nextEntry.getName();
                    nextFile = new File(
                            _destination.getAbsolutePath() + File.separatorChar + destinationFilename);
                    try {
                        nextFile = nextFile.getCanonicalFile();
                    } catch (IOException ignore) {
                        // use original nextFile if getCanonicalFile() fails
                    }
                    nextFile.mkdirs();
                }
            }
        }
    } finally {
        try {
            zFile.close();
        } catch (IOException ignored) {
        }
    }

}

From source file:com.nit.async.DeckTask.java

private TaskData doInBackgroundImportReplace(TaskData... params) {
    // Log.i(AnkiDroidApp.TAG, "doInBackgroundImportReplace");
    Collection col = params[0].getCollection();
    String path = params[0].getString();
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();

    // extract the deck from the zip file
    String fileDir = AnkiDroidApp.getCurrentAnkiDroidDirectory() + "/tmpzip";
    File dir = new File(fileDir);
    if (dir.exists()) {
        BackupManager.removeDir(dir);/*from   ww w.j av a2s. co m*/
    }

    publishProgress(new TaskData(res.getString(R.string.import_unpacking)));
    // from anki2.py
    String colFile = fileDir + "/collection.anki2";
    ZipFile zip;
    try {
        zip = new ZipFile(new File(path));
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - Error while unzipping: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace0");
        return new TaskData(false);
    }
    if (!Utils.unzipFiles(zip, fileDir, new String[] { "collection.anki2", "media" }, null)
            || !(new File(colFile)).exists()) {
        return new TaskData(-2, null, false);
    }

    Collection tmpCol = null;
    try {
        tmpCol = Storage.Collection(colFile);
        if (!tmpCol.validCollection()) {
            tmpCol.close();
            return new TaskData(-2, null, false);
        }
    } finally {
        if (tmpCol != null) {
            tmpCol.close();
        }
    }

    publishProgress(new TaskData(res.getString(R.string.importing_collection)));
    String colPath;
    if (col != null) {
        // unload collection and trigger a backup
        colPath = col.getPath();
        AnkiDroidApp.closeCollection(true);
        BackupManager.performBackup(colPath, true);
    }
    // overwrite collection
    colPath = AnkiDroidApp.getCollectionPath();
    File f = new File(colFile);
    f.renameTo(new File(colPath));
    int addedCount = -1;
    try {
        col = AnkiDroidApp.openCollection(colPath);

        // because users don't have a backup of media, it's safer to import new
        // data and rely on them running a media db check to get rid of any
        // unwanted media. in the future we might also want to duplicate this step
        // import media
        HashMap<String, String> nameToNum = new HashMap<String, String>();
        HashMap<String, String> numToName = new HashMap<String, String>();
        File mediaMapFile = new File(fileDir, "media");
        if (mediaMapFile.exists()) {
            JsonReader jr = new JsonReader(new FileReader(mediaMapFile));
            jr.beginObject();
            String name;
            String num;
            while (jr.hasNext()) {
                num = jr.nextName();
                name = jr.nextString();
                nameToNum.put(name, num);
                numToName.put(num, name);
            }
            jr.endObject();
            jr.close();
        }
        String mediaDir = col.getMedia().getDir();
        int total = nameToNum.size();
        int i = 0;
        for (Map.Entry<String, String> entry : nameToNum.entrySet()) {
            String file = entry.getKey();
            String c = entry.getValue();
            File of = new File(mediaDir, file);
            if (!of.exists()) {
                Utils.unzipFiles(zip, mediaDir, new String[] { c }, numToName);
            }
            ++i;
            publishProgress(new TaskData(res.getString(R.string.import_media_count, (i + 1) * 100 / total)));
        }
        zip.close();
        // delete tmp dir
        BackupManager.removeDir(dir);

        publishProgress(new TaskData(res.getString(R.string.import_update_counts)));
        // Update the counts
        DeckTask.TaskData result = doInBackgroundLoadDeckCounts(new TaskData(col));
        if (result == null) {
            return null;
        }
        return new TaskData(addedCount, result.getObjArray(), true);
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - RuntimeException: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace1");
        return new TaskData(false);
    } catch (FileNotFoundException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - FileNotFoundException: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace2");
        return new TaskData(false);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - IOException: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace3");
        return new TaskData(false);
    }
}

From source file:com.android.sdklib.internal.repository.ArchiveInstaller.java

/**
 * Unzips a zip file into the given destination directory.
 *
 * The archive file MUST have a unique "root" folder.
 * This root folder is skipped when unarchiving.
 *//* w  ww .j a  va 2 s.  co m*/
@SuppressWarnings("unchecked")
private boolean unzipFolder(File archiveFile, long compressedSize, File unzipDestFolder, String description,
        ITaskMonitor monitor) {

    description += " (%1$d%%)";

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);

        // figure if we'll need to set the unix permissions
        boolean usingUnixPerm = SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_DARWIN
                || SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_LINUX;

        // To advance the percent and the progress bar, we don't know the number of
        // items left to unzip. However we know the size of the archive and the size of
        // each uncompressed item. The zip file format overhead is negligible so that's
        // a good approximation.
        long incStep = compressedSize / NUM_MONITOR_INC;
        long incTotal = 0;
        long incCurr = 0;
        int lastPercent = 0;

        byte[] buf = new byte[65536];

        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            String name = entry.getName();

            // ZipFile entries should have forward slashes, but not all Zip
            // implementations can be expected to do that.
            name = name.replace('\\', '/');

            // Zip entries are always packages in a top-level directory
            // (e.g. docs/index.html). However we want to use our top-level
            // directory so we drop the first segment of the path name.
            int pos = name.indexOf('/');
            if (pos < 0 || pos == name.length() - 1) {
                continue;
            } else {
                name = name.substring(pos + 1);
            }

            File destFile = new File(unzipDestFolder, name);

            if (name.endsWith("/")) { //$NON-NLS-1$
                // Create directory if it doesn't exist yet. This allows us to create
                // empty directories.
                if (!destFile.isDirectory() && !destFile.mkdirs()) {
                    monitor.setResult("Failed to create temp directory %1$s", destFile.getPath());
                    return false;
                }
                continue;
            } else if (name.indexOf('/') != -1) {
                // Otherwise it's a file in a sub-directory.
                // Make sure the parent directory has been created.
                File parentDir = destFile.getParentFile();
                if (!parentDir.isDirectory()) {
                    if (!parentDir.mkdirs()) {
                        monitor.setResult("Failed to create temp directory %1$s", parentDir.getPath());
                        return false;
                    }
                }
            }

            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(destFile);
                int n;
                InputStream entryContent = zipFile.getInputStream(entry);
                while ((n = entryContent.read(buf)) != -1) {
                    if (n > 0) {
                        fos.write(buf, 0, n);
                    }
                }
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }

            // if needed set the permissions.
            if (usingUnixPerm && destFile.isFile()) {
                // get the mode and test if it contains the executable bit
                int mode = entry.getUnixMode();
                if ((mode & 0111) != 0) {
                    OsHelper.setExecutablePermission(destFile);
                }
            }

            // Increment progress bar to match. We update only between files.
            for (incTotal += entry.getCompressedSize(); incCurr < incTotal; incCurr += incStep) {
                monitor.incProgress(1);
            }

            int percent = (int) (100 * incTotal / compressedSize);
            if (percent != lastPercent) {
                monitor.setDescription(description, percent);
                lastPercent = percent;
            }

            if (monitor.isCancelRequested()) {
                return false;
            }
        }

        return true;

    } catch (IOException e) {
        monitor.setResult("Unzip failed: %1$s", e.getMessage());

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // pass
            }
        }
    }

    return false;
}

From source file:com.android.sdklib.internal.repository.Archive.java

/**
 * Unzips a zip file into the given destination directory.
 *
 * The archive file MUST have a unique "root" folder. This root folder is skipped when
 * unarchiving. However we return that root folder name to the caller, as it can be used
 * as a template to know what destination directory to use in the Add-on case.
 *//* w  w  w  .  ja v a  2 s. c  o m*/
@SuppressWarnings("unchecked")
private boolean unzipFolder(File archiveFile, long compressedSize, File unzipDestFolder, String description,
        String[] outZipRootFolder, ITaskMonitor monitor) {

    description += " (%1$d%%)";

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);

        // figure if we'll need to set the unix permission
        boolean usingUnixPerm = SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_DARWIN
                || SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_LINUX;

        // To advance the percent and the progress bar, we don't know the number of
        // items left to unzip. However we know the size of the archive and the size of
        // each uncompressed item. The zip file format overhead is negligible so that's
        // a good approximation.
        long incStep = compressedSize / NUM_MONITOR_INC;
        long incTotal = 0;
        long incCurr = 0;
        int lastPercent = 0;

        byte[] buf = new byte[65536];

        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            String name = entry.getName();

            // ZipFile entries should have forward slashes, but not all Zip
            // implementations can be expected to do that.
            name = name.replace('\\', '/');

            // Zip entries are always packages in a top-level directory
            // (e.g. docs/index.html). However we want to use our top-level
            // directory so we drop the first segment of the path name.
            int pos = name.indexOf('/');
            if (pos < 0 || pos == name.length() - 1) {
                continue;
            } else {
                if (outZipRootFolder[0] == null && pos > 0) {
                    outZipRootFolder[0] = name.substring(0, pos);
                }
                name = name.substring(pos + 1);
            }

            File destFile = new File(unzipDestFolder, name);

            if (name.endsWith("/")) { //$NON-NLS-1$
                // Create directory if it doesn't exist yet. This allows us to create
                // empty directories.
                if (!destFile.isDirectory() && !destFile.mkdirs()) {
                    monitor.setResult("Failed to create temp directory %1$s", destFile.getPath());
                    return false;
                }
                continue;
            } else if (name.indexOf('/') != -1) {
                // Otherwise it's a file in a sub-directory.
                // Make sure the parent directory has been created.
                File parentDir = destFile.getParentFile();
                if (!parentDir.isDirectory()) {
                    if (!parentDir.mkdirs()) {
                        monitor.setResult("Failed to create temp directory %1$s", parentDir.getPath());
                        return false;
                    }
                }
            }

            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(destFile);
                int n;
                InputStream entryContent = zipFile.getInputStream(entry);
                while ((n = entryContent.read(buf)) != -1) {
                    if (n > 0) {
                        fos.write(buf, 0, n);
                    }
                }
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }

            // if needed set the permissions.
            if (usingUnixPerm && destFile.isFile()) {
                // get the mode and test if it contains the executable bit
                int mode = entry.getUnixMode();
                if ((mode & 0111) != 0) {
                    setExecutablePermission(destFile);
                }
            }

            // Increment progress bar to match. We update only between files.
            for (incTotal += entry.getCompressedSize(); incCurr < incTotal; incCurr += incStep) {
                monitor.incProgress(1);
            }

            int percent = (int) (100 * incTotal / compressedSize);
            if (percent != lastPercent) {
                monitor.setDescription(description, percent);
                lastPercent = percent;
            }

            if (monitor.isCancelRequested()) {
                return false;
            }
        }

        return true;

    } catch (IOException e) {
        monitor.setResult("Unzip failed: %1$s", e.getMessage());

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // pass
            }
        }
    }

    return false;
}

From source file:at.spardat.xma.xdelta.JarDelta.java

/**
 * Compute delta./*w ww .j a v a 2s  . c om*/
 *
 * @param source the source
 * @param target the target
 * @param output the output
 * @param list the list
 * @param prefix the prefix
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void computeDelta(ZipFile source, ZipFile target, ZipArchiveOutputStream output, PrintWriter list,
        String prefix) throws IOException {
    try {
        for (Enumeration<ZipArchiveEntry> enumer = target.getEntries(); enumer.hasMoreElements();) {
            calculatedDelta = null;
            ZipArchiveEntry targetEntry = enumer.nextElement();
            ZipArchiveEntry sourceEntry = findBestSource(source, target, targetEntry);
            String nextEntryName = prefix + targetEntry.getName();
            if (sourceEntry != null && zipFilesPattern.matcher(sourceEntry.getName()).matches()
                    && !equal(sourceEntry, targetEntry)) {
                nextEntryName += "!";
            }
            nextEntryName += "|" + Long.toHexString(targetEntry.getCrc());
            if (sourceEntry != null) {
                nextEntryName += ":" + Long.toHexString(sourceEntry.getCrc());
            } else {
                nextEntryName += ":0";
            }
            list.println(nextEntryName);
            if (targetEntry.isDirectory()) {
                if (sourceEntry == null) {
                    ZipArchiveEntry outputEntry = entryToNewName(targetEntry, prefix + targetEntry.getName());
                    output.putArchiveEntry(outputEntry);
                    output.closeArchiveEntry();
                }
            } else {
                if (sourceEntry == null || sourceEntry.getSize() <= Delta.DEFAULT_CHUNK_SIZE
                        || targetEntry.getSize() <= Delta.DEFAULT_CHUNK_SIZE) { // new Entry od. alter Eintrag od. neuer Eintrag leer
                    ZipArchiveEntry outputEntry = entryToNewName(targetEntry, prefix + targetEntry.getName());
                    output.putArchiveEntry(outputEntry);
                    try (InputStream in = target.getInputStream(targetEntry)) {
                        int read = 0;
                        while (-1 < (read = in.read(buffer))) {
                            output.write(buffer, 0, read);
                        }
                        output.flush();
                    }
                    output.closeArchiveEntry();
                } else {
                    if (!equal(sourceEntry, targetEntry)) {
                        if (zipFilesPattern.matcher(sourceEntry.getName()).matches()) {
                            File embeddedTarget = File.createTempFile("jardelta-tmp", ".zip");
                            File embeddedSource = File.createTempFile("jardelta-tmp", ".zip");
                            try (FileOutputStream out = new FileOutputStream(embeddedSource);
                                    InputStream in = source.getInputStream(sourceEntry);
                                    FileOutputStream out2 = new FileOutputStream(embeddedTarget);
                                    InputStream in2 = target.getInputStream(targetEntry)) {
                                int read = 0;
                                while (-1 < (read = in.read(buffer))) {
                                    out.write(buffer, 0, read);
                                }
                                out.flush();
                                read = 0;
                                while (-1 < (read = in2.read(buffer))) {
                                    out2.write(buffer, 0, read);
                                }
                                out2.flush();
                                computeDelta(new ZipFile(embeddedSource), new ZipFile(embeddedTarget), output,
                                        list, prefix + sourceEntry.getName() + "!");
                            } finally {
                                embeddedSource.delete();
                                embeddedTarget.delete();
                            }
                        } else {
                            ZipArchiveEntry outputEntry = new ZipArchiveEntry(
                                    prefix + targetEntry.getName() + ".gdiff");
                            outputEntry.setTime(targetEntry.getTime());
                            outputEntry.setComment("" + targetEntry.getCrc());
                            output.putArchiveEntry(outputEntry);
                            if (calculatedDelta != null) {
                                output.write(calculatedDelta);
                                output.flush();
                            } else {
                                try (ByteArrayOutputStream outbytes = new ByteArrayOutputStream()) {
                                    Delta d = new Delta();
                                    DiffWriter diffWriter = new GDiffWriter(new DataOutputStream(outbytes));
                                    int sourceSize = (int) sourceEntry.getSize();
                                    byte[] sourceBytes = new byte[sourceSize];
                                    try (InputStream sourceStream = source.getInputStream(sourceEntry)) {
                                        for (int erg = sourceStream.read(
                                                sourceBytes); erg < sourceBytes.length; erg += sourceStream
                                                        .read(sourceBytes, erg, sourceBytes.length - erg))
                                            ;
                                    }
                                    d.compute(sourceBytes, target.getInputStream(targetEntry), diffWriter);
                                    output.write(outbytes.toByteArray());
                                }
                            }
                            output.closeArchiveEntry();
                        }
                    }
                }
            }
        }
    } finally {
        source.close();
        target.close();
    }
}

From source file:com.android.tools.idea.sdk.remote.internal.archives.ArchiveInstaller.java

/**
 * Unzips a zip file into the given destination directory.
 * <p/>//from w  w w. j  av  a  2 s.co  m
 * The archive file MUST have a unique "root" folder.
 * This root folder is skipped when unarchiving.
 */
@SuppressWarnings("unchecked")
@VisibleForTesting(visibility = Visibility.PRIVATE)
protected boolean unzipFolder(ArchiveReplacement archiveInfo, File archiveFile, File unzipDestFolder,
        ITaskMonitor monitor) {

    Archive newArchive = archiveInfo.getNewArchive();
    RemotePkgInfo pkg = newArchive.getParentPackage();
    String pkgName = pkg.getShortDescription();
    long compressedSize = newArchive.getSize();

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);

        // To advance the percent and the progress bar, we don't know the number of
        // items left to unzip. However we know the size of the archive and the size of
        // each uncompressed item. The zip file format overhead is negligible so that's
        // a good approximation.
        long incStep = compressedSize / NUM_MONITOR_INC;
        long incTotal = 0;
        long incCurr = 0;
        int lastPercent = 0;

        byte[] buf = new byte[65536];

        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            String name = entry.getName();

            // ZipFile entries should have forward slashes, but not all Zip
            // implementations can be expected to do that.
            name = name.replace('\\', '/');

            // Zip entries are always packages in a top-level directory (e.g. docs/index.html).
            int pos = name.indexOf('/');
            if (pos == -1) {
                // All zip entries should have a root folder.
                // This zip entry seems located at the root of the zip.
                // Rather than ignore the file, just place it at the root.
            } else if (pos == name.length() - 1) {
                // This is a zip *directory* entry in the form dir/, so essentially
                // it's the root directory of the SDK. It's safe to ignore that one
                // since we want to use our own root directory and we'll recreate
                // root directories as needed.
                // A direct consequence is that if a malformed archive has multiple
                // root directories, their content will all be merged together.
                continue;
            } else {
                // This is the expected behavior: the zip entry is in the form root/file
                // or root/dir/. We want to use our top-level directory so we drop the
                // first segment of the path name.
                name = name.substring(pos + 1);
            }

            File destFile = new File(unzipDestFolder, name);

            if (name.endsWith("/")) { //$NON-NLS-1$
                // Create directory if it doesn't exist yet. This allows us to create
                // empty directories.
                if (!mFileOp.isDirectory(destFile) && !mFileOp.mkdirs(destFile)) {
                    monitor.logError("Failed to create directory %1$s", destFile.getPath());
                    return false;
                }
                continue;
            } else if (name.indexOf('/') != -1) {
                // Otherwise it's a file in a sub-directory.

                // Sanity check: since we're always unzipping in a fresh temp folder
                // the destination file shouldn't already exist.
                if (mFileOp.exists(destFile)) {
                    monitor.logVerbose("Duplicate file found:  %1$s", name);
                }

                // Make sure the parent directory has been created.
                File parentDir = destFile.getParentFile();
                if (!mFileOp.isDirectory(parentDir)) {
                    if (!mFileOp.mkdirs(parentDir)) {
                        monitor.logError("Failed to create directory %1$s", parentDir.getPath());
                        return false;
                    }
                }
            }

            FileOutputStream fos = null;
            long remains = entry.getSize();
            try {
                fos = new FileOutputStream(destFile);

                // Java bug 4040920: do not rely on the input stream EOF and don't
                // try to read more than the entry's size.
                InputStream entryContent = zipFile.getInputStream(entry);
                int n;
                while (remains > 0
                        && (n = entryContent.read(buf, 0, (int) Math.min(remains, buf.length))) != -1) {
                    remains -= n;
                    if (n > 0) {
                        fos.write(buf, 0, n);
                    }
                }
            } catch (EOFException e) {
                monitor.logError("Error uncompressing file %s. Size: %d bytes, Unwritten: %d bytes.",
                        entry.getName(), entry.getSize(), remains);
                throw e;
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }

            pkg.postUnzipFileHook(newArchive, monitor, mFileOp, destFile, entry);

            // Increment progress bar to match. We update only between files.
            for (incTotal += entry.getCompressedSize(); incCurr < incTotal; incCurr += incStep) {
                monitor.incProgress(1);
            }

            int percent = (int) (100 * incTotal / compressedSize);
            if (percent != lastPercent) {
                monitor.setDescription("Unzipping %1$s (%2$d%%)", pkgName, percent);
                lastPercent = percent;
            }

            if (monitor.isCancelRequested()) {
                return false;
            }
        }

        return true;

    } catch (IOException e) {
        monitor.logError("Unzip failed: %1$s", e.getMessage());

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // pass
            }
        }
    }

    return false;
}

From source file:com.android.sdklib.internal.repository.archives.ArchiveInstaller.java

/**
 * Unzips a zip file into the given destination directory.
 *
 * The archive file MUST have a unique "root" folder.
 * This root folder is skipped when unarchiving.
 *///from   w  ww . ja v  a 2  s .  c  om
@SuppressWarnings("unchecked")
@VisibleForTesting(visibility = Visibility.PRIVATE)
protected boolean unzipFolder(ArchiveReplacement archiveInfo, File archiveFile, File unzipDestFolder,
        ITaskMonitor monitor) {

    Archive newArchive = archiveInfo.getNewArchive();
    Package pkg = newArchive.getParentPackage();
    String pkgName = pkg.getShortDescription();
    long compressedSize = newArchive.getSize();

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(archiveFile);

        // To advance the percent and the progress bar, we don't know the number of
        // items left to unzip. However we know the size of the archive and the size of
        // each uncompressed item. The zip file format overhead is negligible so that's
        // a good approximation.
        long incStep = compressedSize / NUM_MONITOR_INC;
        long incTotal = 0;
        long incCurr = 0;
        int lastPercent = 0;

        byte[] buf = new byte[65536];

        Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
        while (entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();

            String name = entry.getName();

            // ZipFile entries should have forward slashes, but not all Zip
            // implementations can be expected to do that.
            name = name.replace('\\', '/');

            // Zip entries are always packages in a top-level directory (e.g. docs/index.html).
            int pos = name.indexOf('/');
            if (pos == -1) {
                // All zip entries should have a root folder.
                // This zip entry seems located at the root of the zip.
                // Rather than ignore the file, just place it at the root.
            } else if (pos == name.length() - 1) {
                // This is a zip *directory* entry in the form dir/, so essentially
                // it's the root directory of the SDK. It's safe to ignore that one
                // since we want to use our own root directory and we'll recreate
                // root directories as needed.
                // A direct consequence is that if a malformed archive has multiple
                // root directories, their content will all be merged together.
                continue;
            } else {
                // This is the expected behavior: the zip entry is in the form root/file
                // or root/dir/. We want to use our top-level directory so we drop the
                // first segment of the path name.
                name = name.substring(pos + 1);
            }

            File destFile = new File(unzipDestFolder, name);

            if (name.endsWith("/")) { //$NON-NLS-1$
                // Create directory if it doesn't exist yet. This allows us to create
                // empty directories.
                if (!mFileOp.isDirectory(destFile) && !mFileOp.mkdirs(destFile)) {
                    monitor.logError("Failed to create directory %1$s", destFile.getPath());
                    return false;
                }
                continue;
            } else if (name.indexOf('/') != -1) {
                // Otherwise it's a file in a sub-directory.

                // Sanity check: since we're always unzipping in a fresh temp folder
                // the destination file shouldn't already exist.
                if (mFileOp.exists(destFile)) {
                    monitor.logVerbose("Duplicate file found:  %1$s", name);
                }

                // Make sure the parent directory has been created.
                File parentDir = destFile.getParentFile();
                if (!mFileOp.isDirectory(parentDir)) {
                    if (!mFileOp.mkdirs(parentDir)) {
                        monitor.logError("Failed to create directory %1$s", parentDir.getPath());
                        return false;
                    }
                }
            }

            FileOutputStream fos = null;
            long remains = entry.getSize();
            try {
                fos = new FileOutputStream(destFile);

                // Java bug 4040920: do not rely on the input stream EOF and don't
                // try to read more than the entry's size.
                InputStream entryContent = zipFile.getInputStream(entry);
                int n;
                while (remains > 0
                        && (n = entryContent.read(buf, 0, (int) Math.min(remains, buf.length))) != -1) {
                    remains -= n;
                    if (n > 0) {
                        fos.write(buf, 0, n);
                    }
                }
            } catch (EOFException e) {
                monitor.logError("Error uncompressing file %s. Size: %d bytes, Unwritten: %d bytes.",
                        entry.getName(), entry.getSize(), remains);
                throw e;
            } finally {
                if (fos != null) {
                    fos.close();
                }
            }

            pkg.postUnzipFileHook(newArchive, monitor, mFileOp, destFile, entry);

            // Increment progress bar to match. We update only between files.
            for (incTotal += entry.getCompressedSize(); incCurr < incTotal; incCurr += incStep) {
                monitor.incProgress(1);
            }

            int percent = (int) (100 * incTotal / compressedSize);
            if (percent != lastPercent) {
                monitor.setDescription("Unzipping %1$s (%2$d%%)", pkgName, percent);
                lastPercent = percent;
            }

            if (monitor.isCancelRequested()) {
                return false;
            }
        }

        return true;

    } catch (IOException e) {
        monitor.logError("Unzip failed: %1$s", e.getMessage());

    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException e) {
                // pass
            }
        }
    }

    return false;
}

From source file:at.spardat.xma.xdelta.JarPatcher.java

/**
 * Apply delta./*from  w  ww. j av a 2  s.com*/
 *
 * @param patch the patch
 * @param source the source
 * @param output the output
 * @param list the list
 * @param prefix the prefix
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void applyDelta(ZipFile patch, ZipFile source, ZipArchiveOutputStream output, BufferedReader list,
        String prefix) throws IOException {
    String fileName = null;
    try {
        for (fileName = (next == null ? list.readLine()
                : next); fileName != null; fileName = (next == null ? list.readLine() : next)) {
            if (next != null)
                next = null;
            if (!fileName.startsWith(prefix)) {
                next = fileName;
                return;
            }
            int crcDelim = fileName.lastIndexOf(':');
            int crcStart = fileName.lastIndexOf('|');
            long crc = Long.valueOf(fileName.substring(crcStart + 1, crcDelim), 16);
            long crcSrc = Long.valueOf(fileName.substring(crcDelim + 1), 16);
            fileName = fileName.substring(prefix.length(), crcStart);
            if ("META-INF/file.list".equalsIgnoreCase(fileName))
                continue;
            if (fileName.contains("!")) {
                String[] embeds = fileName.split("\\!");
                ZipArchiveEntry original = getEntry(source, embeds[0], crcSrc);
                File originalFile = File.createTempFile("jardelta-tmp-origin-", ".zip");
                File outputFile = File.createTempFile("jardelta-tmp-output-", ".zip");
                Exception thrown = null;
                try (FileOutputStream out = new FileOutputStream(originalFile);
                        InputStream in = source.getInputStream(original)) {
                    int read = 0;
                    while (-1 < (read = in.read(buffer))) {
                        out.write(buffer, 0, read);
                    }
                    out.flush();
                    applyDelta(patch, new ZipFile(originalFile), new ZipArchiveOutputStream(outputFile), list,
                            prefix + embeds[0] + "!");
                } catch (Exception e) {
                    thrown = e;
                    throw e;
                } finally {
                    originalFile.delete();
                    try (FileInputStream in = new FileInputStream(outputFile)) {
                        if (thrown == null) {
                            ZipArchiveEntry outEntry = copyEntry(original);
                            output.putArchiveEntry(outEntry);
                            int read = 0;
                            while (-1 < (read = in.read(buffer))) {
                                output.write(buffer, 0, read);
                            }
                            output.flush();
                            output.closeArchiveEntry();
                        }
                    } finally {
                        outputFile.delete();
                    }
                }
            } else {
                try {
                    ZipArchiveEntry patchEntry = getEntry(patch, prefix + fileName, crc);
                    if (patchEntry != null) { // new Entry
                        ZipArchiveEntry outputEntry = JarDelta.entryToNewName(patchEntry, fileName);
                        output.putArchiveEntry(outputEntry);
                        if (!patchEntry.isDirectory()) {
                            try (InputStream in = patch.getInputStream(patchEntry)) {
                                int read = 0;
                                while (-1 < (read = in.read(buffer))) {
                                    output.write(buffer, 0, read);
                                }
                            }
                        }
                        closeEntry(output, outputEntry, crc);
                    } else {
                        ZipArchiveEntry sourceEntry = getEntry(source, fileName, crcSrc);
                        if (sourceEntry == null) {
                            throw new FileNotFoundException(
                                    fileName + " not found in " + sourceName + " or " + patchName);
                        }
                        if (sourceEntry.isDirectory()) {
                            ZipArchiveEntry outputEntry = new ZipArchiveEntry(sourceEntry);
                            output.putArchiveEntry(outputEntry);
                            closeEntry(output, outputEntry, crc);
                            continue;
                        }
                        patchEntry = getPatchEntry(patch, prefix + fileName + ".gdiff", crc);
                        if (patchEntry != null) { // changed Entry
                            ZipArchiveEntry outputEntry = new ZipArchiveEntry(sourceEntry);
                            outputEntry.setTime(patchEntry.getTime());
                            output.putArchiveEntry(outputEntry);
                            byte[] sourceBytes = new byte[(int) sourceEntry.getSize()];
                            try (InputStream sourceStream = source.getInputStream(sourceEntry)) {
                                for (int erg = sourceStream
                                        .read(sourceBytes); erg < sourceBytes.length; erg += sourceStream
                                                .read(sourceBytes, erg, sourceBytes.length - erg))
                                    ;
                            }
                            InputStream patchStream = patch.getInputStream(patchEntry);
                            GDiffPatcher diffPatcher = new GDiffPatcher();
                            diffPatcher.patch(sourceBytes, patchStream, output);
                            patchStream.close();
                            outputEntry.setCrc(crc);
                            closeEntry(output, outputEntry, crc);
                        } else { // unchanged Entry
                            ZipArchiveEntry outputEntry = new ZipArchiveEntry(sourceEntry);
                            output.putArchiveEntry(outputEntry);
                            try (InputStream in = source.getInputStream(sourceEntry)) {
                                int read = 0;
                                while (-1 < (read = in.read(buffer))) {
                                    output.write(buffer, 0, read);
                                }
                            }
                            output.flush();
                            closeEntry(output, outputEntry, crc);
                        }
                    }
                } catch (PatchException pe) {
                    IOException ioe = new IOException();
                    ioe.initCause(pe);
                    throw ioe;
                }
            }
        }
    } catch (Exception e) {
        System.err.println(prefix + fileName);
        throw e;
    } finally {
        source.close();
        output.close();
    }
}

From source file:ee.sk.digidoc.factory.SAXDigiDocFactory.java

/**
 * Reads in a DigiDoc file. One of fname or isSdoc must be given.
 * @param fname signed doc filename//from  w  w  w  .j  a  v a  2s. com
 * @param isSdoc opened stream with DigiDoc data
 * The user must open and close it.
 * @param errs list of errors to fill with parsing errors. If given
 * then attempt is made to continue parsing on errors and return them in this list.
 * If not given (null) then the first error found will be thrown.
 * @return signed document object if successfully parsed
 */
private SignedDoc readSignedDocOfType(String fname, InputStream isSdoc, boolean isBdoc, List errs)
        throws DigiDocException {
    // Use an instance of ourselves as the SAX event handler
    SAXDigiDocFactory handler = this;
    m_errs = errs;
    DigiDocVerifyFactory.initProvider();
    SAXParserFactory factory = SAXParserFactory.newInstance();
    if (m_logger.isDebugEnabled())
        m_logger.debug("Start reading ddoc/bdoc " + ((fname != null) ? "from file: " + fname : "from stream")
                + " bdoc: " + isBdoc);
    if (fname == null && isSdoc == null) {
        throw new DigiDocException(DigiDocException.ERR_READ_FILE, "No input file", null);
    }
    if (fname != null) {
        File inFile = new File(fname);
        if (!inFile.canRead() || inFile.length() == 0) {
            throw new DigiDocException(DigiDocException.ERR_READ_FILE, "Empty or unreadable input file", null);
        }
    }
    ZipFile zf = null;
    ZipArchiveInputStream zis = null;
    ZipArchiveEntry ze = null;
    InputStream isEntry = null;
    File fTmp = null;
    try {
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        if (isBdoc) { // bdoc parsing
            // must be a bdoc document ?
            m_doc = new SignedDoc();
            m_doc.setVersion(SignedDoc.BDOC_VERSION_1_0);
            m_doc.setFormat(SignedDoc.FORMAT_BDOC);
            Enumeration eFiles = null;
            if (fname != null) {
                zf = new ZipFile(fname, "UTF-8");
                eFiles = zf.getEntries();
            } else if (isSdoc != null) {
                zis = new ZipArchiveInputStream(isSdoc, "UTF-8", true, true);
            }
            ArrayList lSigFnames = new ArrayList();
            ArrayList lDataFnames = new ArrayList();
            // read all entries
            boolean bHasMimetype = false, bManifest1 = false;
            int nFil = 0;
            while ((zf != null && eFiles.hasMoreElements())
                    || (zis != null && ((ze = zis.getNextZipEntry()) != null))) {
                nFil++;

                // read entry
                if (zf != null) { // ZipFile
                    ze = (ZipArchiveEntry) eFiles.nextElement();
                    isEntry = zf.getInputStream(ze);
                } else { // ZipArchiveInputStream
                    int n = 0, nTot = 0;
                    if ((ze.getName().equals(FILE_MIMETYPE) || ze.getName().equals(FILE_MANIFEST)
                            || (ze.getName().startsWith(FILE_SIGNATURES) && ze.getName().endsWith(".xml")))
                            || (nMaxBdocFilCached <= 0
                                    || (ze.getSize() < nMaxBdocFilCached && ze.getSize() >= 0))) {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        byte[] data = new byte[2048];
                        while ((n = zis.read(data)) > 0) {
                            bos.write(data, 0, n);
                            nTot += n;
                        }
                        if (m_logger.isDebugEnabled())
                            m_logger.debug("Read: " + nTot + " bytes from zip");
                        data = bos.toByteArray();
                        bos = null;
                        isEntry = new ByteArrayInputStream(data);
                    } else {
                        File fCacheDir = new File(ConfigManager.instance().getStringProperty(
                                "DIGIDOC_DF_CACHE_DIR", System.getProperty("java.io.tmpdir")));
                        fTmp = File.createTempFile("bdoc-data", ".tmp", fCacheDir);
                        FileOutputStream fos = new FileOutputStream(fTmp);
                        byte[] data = new byte[2048];
                        while ((n = zis.read(data)) > 0) {
                            fos.write(data, 0, n);
                            nTot += n;
                        }
                        if (m_logger.isDebugEnabled())
                            m_logger.debug("Read: " + nTot + " bytes from zip to: " + fTmp.getAbsolutePath());
                        fos.close();
                        isEntry = new FileInputStream(fTmp);
                    }
                }
                if (m_logger.isDebugEnabled())
                    m_logger.debug("Entry: " + ze.getName() + " nlen: " + ze.getName().length() + " size: "
                            + ze.getSize() + " dir: " + ze.isDirectory() + " comp-size: "
                            + ze.getCompressedSize());
                // mimetype file
                if (ze.getName().equals(FILE_MIMETYPE)) {
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Check mimetype!");
                    checkBdocMimetype(isEntry);
                    bHasMimetype = true;
                    m_doc.setComment(ze.getComment());
                    if (nFil != 1) {
                        m_logger.error("mimetype file is " + nFil + " file but must be first");
                        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                "mimetype file is not first zip entry", null));
                    }
                } else if (ze.getName().equals(FILE_MANIFEST)) { // manifest.xml file
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Read manifest");
                    if (!bManifest1 && isEntry != null) {
                        bManifest1 = true;
                        BdocManifestParser mfparser = new BdocManifestParser(m_doc);
                        mfparser.readManifest(isEntry);
                    } else {
                        m_logger.error("Found multiple manifest.xml files!");
                        throw new DigiDocException(DigiDocException.ERR_MULTIPLE_MANIFEST_FILES,
                                "Found multiple manifest.xml files!", null);
                    }
                } else if (ze.getName().startsWith(FILE_SIGNATURES) && ze.getName().endsWith(".xml")) { // some signature
                    m_fileName = ze.getName();
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Reading bdoc siganture: " + m_fileName);
                    boolean bExists = false;
                    for (int j = 0; j < lSigFnames.size(); j++) {
                        String s1 = (String) lSigFnames.get(j);
                        if (s1.equals(m_fileName))
                            bExists = true;
                    }
                    if (bExists) {
                        m_logger.error("Duplicate signature filename: " + m_fileName);
                        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                "Duplicate signature filename: " + m_fileName, null));
                    } else
                        lSigFnames.add(m_fileName);
                    SAXParser saxParser = factory.newSAXParser();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    int n = 0;
                    byte[] data = new byte[2048];
                    while ((n = isEntry.read(data)) > 0)
                        bos.write(data, 0, n);
                    data = bos.toByteArray();
                    bos = null;
                    if (m_logger.isDebugEnabled())
                        m_logger.debug(
                                "Parsing bdoc: " + m_fileName + " size: " + ((data != null) ? data.length : 0));
                    saxParser.parse(new SignatureInputStream(new ByteArrayInputStream(data)), this);
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Parsed bdoc: " + m_fileName);
                    Signature sig1 = m_doc.getLastSignature();
                    m_sigComment = ze.getComment();
                    if (sig1 != null) {
                        sig1.setPath(m_fileName);
                        sig1.setComment(ze.getComment());
                    }
                } else { // probably a data file
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Read data file: " + ze.getName());
                    if (!ze.isDirectory()) {
                        boolean bExists = false;
                        for (int j = 0; j < lDataFnames.size(); j++) {
                            String s1 = (String) lDataFnames.get(j);
                            if (s1.equals(ze.getName()))
                                bExists = true;
                        }
                        if (bExists) {
                            m_logger.error("Duplicate datafile filename: " + ze.getName());
                            handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                    "Duplicate datafile filename: " + ze.getName(), null));
                        } else
                            lDataFnames.add(ze.getName());
                        DataFile df = m_doc.findDataFileById(ze.getName());
                        if (df != null) {
                            if (ze.getSize() > 0)
                                df.setSize(ze.getSize());
                            df.setContentType(DataFile.CONTENT_BINARY);
                            df.setFileName(ze.getName());
                        } else {
                            df = new DataFile(ze.getName(), DataFile.CONTENT_BINARY, ze.getName(),
                                    "application/binary", m_doc);
                            if (m_doc.getDataFiles() == null)
                                m_doc.setDataFiles(new ArrayList());
                            m_doc.getDataFiles().add(df);
                            //m_doc.addDataFile(df); // this does some intiailization work unnecessary here
                        }
                        // enable caching if requested
                        if (isEntry != null)
                            df.setOrCacheBodyAndCalcHashes(isEntry);
                        df.setComment(ze.getComment());
                        df.setLastModDt(new Date(ze.getTime()));
                        // fix mime type according to DataObjectFormat
                        Signature sig1 = m_doc.getLastSignature();
                        if (sig1 != null) {
                            Reference dRef = sig1.getSignedInfo().getReferenceForDataFile(df);
                            if (dRef != null) {
                                DataObjectFormat dof = sig1.getSignedInfo()
                                        .getDataObjectFormatForReference(dRef);
                                if (dof != null) {
                                    df.setMimeType(dof.getMimeType());
                                }
                            }
                        }
                    }
                }
                if (fTmp != null) {
                    fTmp.delete();
                    fTmp = null;
                }
            } // while zip entries
            if (!bHasMimetype) {
                m_logger.error("No mimetype file");
                handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                        "Not a BDOC format file! No mimetype file!", null));
            }
            // if no signatures exist then copy mime-type from manifest.xml to DataFile -s
            if (m_doc.countSignatures() == 0) {
                for (int i = 0; i < m_doc.countDataFiles(); i++) {
                    DataFile df = m_doc.getDataFile(i);
                    if (m_doc.getManifest() != null) {
                        for (int j = 0; j < m_doc.getManifest().getNumFileEntries(); j++) {
                            ManifestFileEntry mfe = m_doc.getManifest().getFileEntry(j);
                            if (mfe.getFullPath() != null && mfe.getFullPath().equals(df.getFileName())) {
                                df.setMimeType(mfe.getMediaType());
                            } // if fullpath
                        } // for
                    } // if
                } // for i
            }
        } else { // ddoc parsing
            if (m_logger.isDebugEnabled())
                m_logger.debug("Reading ddoc: " + fname + " file: " + m_fileName);
            m_fileName = fname;
            SAXParser saxParser = factory.newSAXParser();
            if (fname != null)
                saxParser.parse(new SignatureInputStream(new FileInputStream(fname)), this);
            else if (isSdoc != null)
                saxParser.parse(isSdoc, this);
        }
    } catch (org.xml.sax.SAXParseException ex) {
        m_logger.error("SAX Error: " + ex);
        handleError(ex);

    } catch (Exception ex) {
        m_logger.error("Error reading3: " + ex);
        ex.printStackTrace();
        /*if(ex instanceof DigiDocException){
           DigiDocException dex = (DigiDocException)ex;
           m_logger.error("Dex: " + ex);
           if(dex.getNestedException() != null) {
              dex.getNestedException().printStackTrace();
              m_logger.error("Trace: "); 
           }
        }*/
        handleError(ex);
    } finally { // cleanup
        try {
            if (isEntry != null) {
                isEntry.close();
                isEntry = null;
            }
            if (zis != null)
                zis.close();
            if (zf != null)
                zf.close();
            if (fTmp != null) {
                fTmp.delete();
                fTmp = null;
            }
        } catch (Exception ex) {
            m_logger.error("Error closing streams and files: " + ex);
        }
    }
    // compare Manifest and DataFiles
    boolean bErrList = (errs != null);
    if (errs == null)
        errs = new ArrayList();
    boolean bOk = DigiDocVerifyFactory.verifyManifestEntries(m_doc, errs);
    if (m_doc == null) {
        m_logger.error("Error reading4: doc == null");
        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                "This document is not in ddoc or bdoc format", null));
    }
    if (!bErrList && errs.size() > 0) { // if error list was not used then we have to throw exception. So we will throw the first one since we can only do it once
        DigiDocException ex = (DigiDocException) errs.get(0);
        throw ex;
    }
    return m_doc;
}