Example usage for java.util.zip ZipOutputStream STORED

List of usage examples for java.util.zip ZipOutputStream STORED

Introduction

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

Prototype

int STORED

To view the source code for java.util.zip ZipOutputStream STORED.

Click Source Link

Document

Compression method for uncompressed (STORED) entries.

Usage

From source file:org.opentestsystem.authoring.testitembank.service.impl.ApipZipInputFileExtractorService.java

private Map<String, ApipItemContent> getItemZipMap(final ApipManifest manifest, final ZipFile originalZip,
        final HashMap<String, ZipArchiveEntry> zipEntryMap) {
    final Map<String, ApipManifestResource> resourceMap = manifest.getResourceMap();
    final Map<String, ApipItemContent> itemContentMap = new HashMap<String, ApipItemContent>();
    final String apipRoot = findApipRootDirectoy(zipEntryMap);
    for (final ApipManifestResource resource : manifest.getResources()) {
        if (isItemResource(resource.getResourceType())) {
            final ApipItemContent content = new ApipItemContent();
            ZipOutputStream zipOut = null;
            ByteArrayOutputStream byteStream = null;
            byte[] bytes = null;
            try {
                byteStream = new ByteArrayOutputStream();
                zipOut = new ZipOutputStream(byteStream);
                zipOut.setLevel(ZipOutputStream.STORED);
                final ApipManifest itemManifest = new ApipManifest();
                itemManifest.setIdentifier(manifest.getIdentifier() + "_" + resource.getIdentifier());
                itemManifest.setSchema(manifest.getSchema());
                itemManifest.setSchemaVersion(manifest.getSchemaVersion());

                final List<ApipManifestResource> requiredResources = new ArrayList<ApipManifestResource>();
                requiredResources.add(resource);

                final ZipArchiveEntry itemEntry = zipEntryMap
                        .get(apipRoot + resource.getFileReference().getHref());
                copyZipEntry(itemEntry, originalZip, zipOut);
                for (final ApipManifestDependency dependency : resource.getDependencies()) {
                    final ApipManifestResource dependencyResource = resourceMap.get(dependency.getIdentifier());
                    if (dependencyResource != null) {
                        final ZipArchiveEntry dependencyEntry = zipEntryMap
                                .get(apipRoot + dependencyResource.getFileReference().getHref());
                        if (dependencyEntry == null) {
                            if (ResourceTypePrefix.apipMetadata
                                    .isPrefixFor(dependencyResource.getResourceType())
                                    || ResourceTypePrefix.apipItem
                                            .isPrefixFor(dependencyResource.getResourceType())
                                    || ResourceTypePrefix.stimuli
                                            .isPrefixFor(dependencyResource.getResourceType())) {
                                content.addError("item.invalid.zip.missingResource",
                                        new String[] { resource.getIdentifier(), dependency.getIdentifier() });
                            } else
                                LOGGER.warn(String.format("Zip missing resource: %s, %s ",
                                        resource.getIdentifier(), dependency.getIdentifier()));

                        } else {
                            requiredResources.add(dependencyResource);
                            copyZipEntry(dependencyEntry, originalZip, zipOut);
                        }//from ww w  .j  a  v  a 2 s  .  com
                    }
                }
                itemManifest.setResources(requiredResources);

                final ZipEntry entry = new ZipEntry(apipRoot + IMS_MANIFEST_NAME);
                zipOut.putNextEntry(entry);

                zipXMLService.writeToManifestToOutputStream(itemManifest, zipOut);
                zipOut.closeEntry();

                zipOut.flush();
                zipOut.close();
                bytes = byteStream.toByteArray();
                content.setItemZip(bytes);
            } catch (final Exception e) {
                LOGGER.error("creating item zip for " + resource.getIdentifier(), e);
            } finally {
                try {
                    if (zipOut != null) {
                        zipOut.flush();
                        zipOut.close();
                    }
                    if (byteStream != null) {
                        byteStream.flush();
                        byteStream.close();
                    }
                } catch (final IOException e) {
                    LOGGER.error("error closing zip.", e);
                }
            }
            itemContentMap.put(resource.getIdentifier(), content);
        }
    }
    return itemContentMap;

}

From source file:org.sakaiproject.search.util.FileUtils.java

/**
 * pack a segment into the zip//from   www. j av a  2s  .c o  m
 * @param compress 
 * 
 * @param addsi
 * @return
 * @throws IOException
 */
public static void pack(File source, final String basePath, final String replacePath, OutputStream output,
        boolean compress) throws IOException {
    log.debug("Packing " + source + " repacing " + basePath + " with " + replacePath);
    final ZipOutputStream zout = new ZipOutputStream(output);
    if (compress) {
        zout.setLevel(ZipOutputStream.DEFLATED);
    } else {
        zout.setLevel(ZipOutputStream.STORED);
    }
    final byte[] buffer = new byte[1024 * 100];
    try {
        recurse(source, new RecurseAction() {

            public void doFile(File file) throws IOException {
                if (!file.isDirectory()) {
                    log.debug("               Add " + file.getPath());
                    addSingleFile(basePath, replacePath, file, zout, buffer);
                } else {
                    log.debug("              Ignore " + file.getPath());
                }
            }

            public void doBeforeFile(File f) {
            }

            public void doAfterFile(File f) {
            }

        });
    } finally {
        zout.flush();
        try {
            zout.close();
        } catch (Exception e) {
            log.warn("Exception closing output zip", e);
        }
    }
}

From source file:org.sonarsource.commandlinezip.ZipUtils7.java

public static void smartReportZip(final Path srcDir, Path zip) throws IOException {
    try (final OutputStream out = FileUtils.openOutputStream(zip.toFile());
            final ZipOutputStream zout = new ZipOutputStream(out)) {
        Files.walkFileTree(srcDir, new SimpleFileVisitor<Path>() {
            @Override/* w w w  .  j  ava2s .c  o m*/
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                try (InputStream in = new BufferedInputStream(new FileInputStream(file.toFile()))) {
                    String entryName = srcDir.relativize(file).toString();
                    int level = file.toString().endsWith(".pb") ? ZipOutputStream.STORED
                            : Deflater.DEFAULT_COMPRESSION;
                    zout.setLevel(level);
                    ZipEntry entry = new ZipEntry(entryName);
                    zout.putNextEntry(entry);
                    IOUtils.copy(in, zout);
                    zout.closeEntry();
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                if (dir.equals(srcDir)) {
                    return FileVisitResult.CONTINUE;
                }

                String entryName = srcDir.relativize(dir).toString();
                ZipEntry entry = new ZipEntry(entryName);
                zout.putNextEntry(entry);
                zout.closeEntry();
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

From source file:org.sonarsource.commandlinezip.ZipUtils7.java

public static void fastZip(final Path srcDir, Path zip) throws IOException {
    try (final OutputStream out = FileUtils.openOutputStream(zip.toFile());
            final ZipOutputStream zout = new ZipOutputStream(out)) {
        zout.setMethod(ZipOutputStream.STORED);
        Files.walkFileTree(srcDir, new SimpleFileVisitor<Path>() {
            @Override/*from  w  w  w .j  ava2  s . c  o  m*/
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                try (InputStream in = new BufferedInputStream(new FileInputStream(file.toFile()))) {
                    String entryName = srcDir.relativize(file).toString();
                    ZipEntry entry = new ZipEntry(entryName);
                    zout.putNextEntry(entry);
                    IOUtils.copy(in, zout);
                    zout.closeEntry();
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                if (dir.equals(srcDir)) {
                    return FileVisitResult.CONTINUE;
                }

                String entryName = srcDir.relativize(dir).toString();
                ZipEntry entry = new ZipEntry(entryName);
                zout.putNextEntry(entry);
                zout.closeEntry();
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

From source file:se.unlogic.hierarchy.foregroundmodules.imagegallery.GalleryModule.java

@WebPublic(alias = "download")
public SimpleForegroundModuleResponse downloadGallery(HttpServletRequest req, HttpServletResponse res,
        User user, URIParser uriParser) throws SQLException, URINotFoundException {

    Gallery gallery = null;/*  w w w .j a  v a2 s. c  o  m*/

    if (uriParser.size() < 3 || (gallery = this.galleryDao.get(uriParser.get(2))) == null) {

        throw new URINotFoundException(uriParser);

    }

    // check if path is valid
    File dir = new File(gallery.getUrl());

    if (!dir.canRead()) {
        throw new URINotFoundException(uriParser);
    }

    log.info("User " + user + " downloading gallery " + gallery + "...");

    long startTime = System.currentTimeMillis();

    File[] files = dir.listFiles(fileFilter);

    ZipOutputStream zipOutputStream = null;

    try {
        res.setContentType("application/zip");
        res.setHeader("Content-Disposition",
                "inline; filename=\"" + FileUtils.toValidHttpFilename(gallery.getName()) + ".zip\"");
        res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate");

        zipOutputStream = new ZipOutputStream(res.getOutputStream());
        zipOutputStream.setLevel(ZipOutputStream.STORED);

        ZipUtils.addFiles(files, zipOutputStream);

        zipOutputStream.flush();

        log.info("Sent gallery " + gallery + " containing " + files.length + " files to user " + user + " in "
                + TimeUtils.millisecondsToString(System.currentTimeMillis() - startTime));

    } catch (IOException e) {

        log.info("Error sending gallery " + gallery + " to user " + user);

    } finally {

        StreamUtils.closeStream(zipOutputStream);
    }

    return null;
}