Example usage for java.util.zip ZipEntry ZipEntry

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

Introduction

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

Prototype

public ZipEntry(ZipEntry e) 

Source Link

Document

Creates a new zip entry with fields taken from the specified zip entry.

Usage

From source file:io.neba.core.logviewer.LogfileViewerConsolePlugin.java

/**
 * Streams the contents of the log directory as a zip file.
 *///  www.j  ava2  s.  c o  m
private void download(HttpServletResponse res, HttpServletRequest req) throws IOException {
    final String selectedLogfile = req.getParameter("file");
    final String filenameSuffix = isEmpty(selectedLogfile) ? ""
            : "-" + substringAfterLast(selectedLogfile, File.separator);

    res.setContentType("application/zip");
    res.setHeader("Content-Disposition",
            "attachment;filename=logfiles-" + req.getServerName() + filenameSuffix + ".zip");
    ZipOutputStream zos = new ZipOutputStream(res.getOutputStream());
    try {
        for (File file : this.logFiles.resolveLogFiles()) {
            if (selectedLogfile != null && !file.getAbsolutePath().equals(selectedLogfile)) {
                continue;
            }

            ZipEntry ze = new ZipEntry(toZipFileEntryName(file));
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream(file);
            try {
                copy(in, zos);
                zos.closeEntry();
            } finally {
                closeQuietly(in);
            }
        }
        zos.finish();
    } finally {
        closeQuietly(zos);
    }
}

From source file:io.wcm.tooling.commons.contentpackagebuilder.ContentPackage.java

/**
 * Read template file from classpath, replace variables and store it in the zip stream.
 * @param path Path/* w  w  w.  j  a v  a2  s . c  om*/
 * @throws IOException
 */
private void buildTemplatedMetadataFile(String path) throws IOException {
    try (InputStream is = getClass().getResourceAsStream("/content-package-template/" + path)) {
        String xmlContent = IOUtils.toString(is);
        for (Map.Entry<String, Object> entry : metadata.getVars().entrySet()) {
            xmlContent = StringUtils.replace(xmlContent, "{{" + entry.getKey() + "}}",
                    StringEscapeUtils.escapeXml(entry.getValue().toString()));
        }
        zip.putNextEntry(new ZipEntry(path));
        try {
            zip.write(xmlContent.getBytes(Charsets.UTF_8));
        } finally {
            zip.closeEntry();
        }
    }
}

From source file:es.gob.afirma.signers.ooxml.be.fedict.eid.applet.service.signer.ooxml.AbstractOOXMLSignatureService.java

private static void addOriginSigs(final ZipOutputStream zipOutputStream) throws IOException {
    zipOutputStream.putNextEntry(new ZipEntry("_xmlsignatures/origin.sigs")); //$NON-NLS-1$
}

From source file:dalma.container.ClassLoaderImpl.java

/**
 * Add a file to the path. This classloader reads the manifest, if
 * available, and adds any additional class path jars specified in the
 * manifest.// w w w . j a  v  a  2s.  c o m
 *
 * @param pathComponent the file which is to be added to the path for
 *                      this class loader
 *
 * @throws IOException if data needed from the file cannot be read.
 */
public void addPathFile(File pathComponent) throws IOException {
    pathComponents.addElement(pathComponent);

    if (pathComponent.isDirectory()) {
        return;
    }

    String absPathPlusTimeAndLength = pathComponent.getAbsolutePath() + pathComponent.lastModified() + "-"
            + pathComponent.length();
    String classpath = pathMap.get(absPathPlusTimeAndLength);
    if (classpath == null) {
        ZipFile jarFile = null;
        InputStream manifestStream = null;
        try {
            jarFile = new ZipFile(pathComponent);
            manifestStream = jarFile.getInputStream(new ZipEntry("META-INF/MANIFEST.MF"));

            if (manifestStream == null) {
                return;
            }
            Manifest manifest = new Manifest(manifestStream);
            classpath = manifest.getMainAttributes().getValue("Class-Path");

        } finally {
            if (manifestStream != null) {
                manifestStream.close();
            }
            if (jarFile != null) {
                jarFile.close();
            }
        }
        if (classpath == null) {
            classpath = "";
        }
        pathMap.put(absPathPlusTimeAndLength, classpath);
    }

    if (!"".equals(classpath)) {
        URL baseURL = pathComponent.toURL();
        StringTokenizer st = new StringTokenizer(classpath);
        while (st.hasMoreTokens()) {
            String classpathElement = st.nextToken();
            URL libraryURL = new URL(baseURL, classpathElement);
            if (!libraryURL.getProtocol().equals("file")) {
                logger.fine("Skipping jar library " + classpathElement
                        + " since only relative URLs are supported by this" + " loader");
                continue;
            }
            File libraryFile = new File(libraryURL.getFile());
            if (libraryFile.exists() && !isInPath(libraryFile)) {
                addPathFile(libraryFile);
            }
        }
    }
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Compresses the input path name into a ZIP output file stream.  The
 * method will recursively process all files and sub folders within
 * the input path file.// w  ww  . j  av a2s. co  m
 *
 * @param aPathName Identifies the name of the folder to compress.
 * @param aZipOutStream A previously opened ZIP output file stream.
 * @throws IOException Related to opening the file streams and
 * related read/write operations.
 */
static public void zipPath(String aPathName, ZipOutputStream aZipOutStream) throws IOException {
    int byteCount;
    byte[] ioBuf;
    String[] fileList;
    File zipFile, inFile;
    FileInputStream fileIn;

    zipFile = new File(aPathName);
    if (!zipFile.isDirectory())
        return;

    fileList = zipFile.list();
    if (fileList == null)
        return;

    for (String fileName : fileList) {
        inFile = new File(aPathName, fileName);
        if (inFile.isDirectory())
            zipPath(inFile.getPath(), aZipOutStream);
        else {
            ioBuf = new byte[FILE_IO_BUFFER_SIZE];
            fileIn = new FileInputStream(inFile);
            aZipOutStream.putNextEntry(new ZipEntry(inFile.getName()));
            byteCount = fileIn.read(ioBuf);
            while (byteCount > 0) {
                aZipOutStream.write(ioBuf, 0, byteCount);
                byteCount = fileIn.read(ioBuf);
            }
            fileIn.close();
        }
    }
}

From source file:edu.harvard.iq.dvn.core.analysis.NetworkDataServiceBean.java

private void addZipEntry(ZipOutputStream zout, String inputFileName, String outputFileName) throws IOException {
    FileInputStream tmpin = new FileInputStream(inputFileName);
    byte[] dataBuffer = new byte[8192];
    int i = 0;/*  ww w .j a v a  2s. c o m*/

    ZipEntry e = new ZipEntry(outputFileName);
    zout.putNextEntry(e);

    while ((i = tmpin.read(dataBuffer)) > 0) {
        zout.write(dataBuffer, 0, i);
        zout.flush();
    }
    tmpin.close();
    zout.closeEntry();
}

From source file:org.openremote.beehive.configuration.www.UsersAPI.java

private void writeZipEntry(ZipOutputStream zipOutput, File file, java.nio.file.Path basePath)
        throws IOException {
    ZipEntry entry = new ZipEntry(basePath.relativize(file.toPath()).toString());
    entry.setSize(file.length());// w w  w. j a  va  2 s  .  co m
    entry.setTime(file.lastModified());
    zipOutput.putNextEntry(entry);

    IOUtils.copy(new FileInputStream(file), zipOutput);

    zipOutput.flush();
    zipOutput.closeEntry();
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

/**
 * @param zipFile/*from  ww  w  .  j a v a 2 s .  c o  m*/
 * @param files
 * @throws java.io.IOException
 */
public static void addFilesToZip(File zipFile, Map<String, File> files) throws IOException {
    // get a temp file
    File tempFile = File.createTempFile(zipFile.getName(), null);
    // delete it, otherwise you cannot rename your existing zip to it.
    FileUtils.deleteQuietly(tempFile);

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (Map.Entry<String, File> e : files.entrySet()) {
            if (e.getKey().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {
            out.putNextEntry(new ZipEntry(name));
            IOUtils.copy(zin, out);
        }
        entry = zin.getNextEntry();
    }
    // Close the streams
    zin.close();
    // Compress the files
    for (Map.Entry<String, File> e : files.entrySet()) {
        InputStream in = new FileInputStream(e.getValue());
        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(e.getKey()));
        // Transfer bytes from the file to the ZIP file

        IOUtils.copy(in, out);
        // Complete the entry
        out.closeEntry();
        IOUtils.closeQuietly(in);
    }
    // Complete the ZIP file
    IOUtils.closeQuietly(out);
    FileUtils.deleteQuietly(tempFile);
}

From source file:edu.harvard.med.screensaver.service.cherrypicks.CherryPickRequestPlateMapFilesBuilder.java

/**
 * for  [#3206] Generate a distinct plate/copy list for CPR download file
 *//* ww w.ja  va2 s  . co  m*/
private void buildDistinctPlateCopyFile(CherryPickRequest cherryPickRequest,
        Set<CherryPickAssayPlate> forPlates, ZipOutputStream zipOut) throws IOException {
    PrintWriter out = new CSVPrintWriter(new OutputStreamWriter(zipOut), NEWLINE);
    zipOut.putNextEntry(new ZipEntry(DISTINCT_PLATE_COPY_LIST_FILE_NAME));
    for (String string : DISTINCT_PLATECOPYLIST_FILE_HEADERS) {
        out.print(string);
    }
    out.println();

    // TODO: consider caching this while building the CPR
    Set<Plate> sourcePlates = Sets.newHashSet();

    for (CherryPickAssayPlate plate : forPlates) {
        for (LabCherryPick lcp : plate.getLabCherryPicks()) {
            lcp = genericEntityDao.reloadEntity(lcp, true);
            sourcePlates.add(librariesDao.findPlate(lcp.getSourceWell().getPlateNumber(),
                    lcp.getSourceCopy().getName()));
        }
    }
    // sort by plate/copy/location
    List<Plate> list = Lists.newArrayList(sourcePlates);
    Collections.sort(list, new Comparator<Plate>() {
        @Override
        public int compare(Plate o1, Plate o2) {
            if (o1.equals(o2)) {
                return o1.getLocation().compareTo(o2.getLocation());
            }
            return o1.compareTo(o2); // note Plate.compare() does a plate/copy compare
        }
    });
    for (Plate p : list) {
        out.print(p.getPlateNumber());
        out.print(p.getCopy().getName());
        out.print(p.getLocation() == null ? "" : p.getLocation().toDisplayString());
        out.println();
    }
}

From source file:com.googlecode.clearnlp.component.AbstractStatisticalComponent.java

protected void saveWeightVector(ZipOutputStream zout, String entryName) throws Exception {
    int i, size = s_models.length;
    ObjectOutputStream oout;// w  w w. j  a  v  a 2  s. c o  m

    for (i = 0; i < size; i++) {
        zout.putNextEntry(new ZipEntry(entryName + i));
        oout = new ObjectOutputStream(new BufferedOutputStream(zout));
        s_models[i].saveWeightVector(oout);
        oout.flush();
        zout.closeEntry();
    }
}