Example usage for java.util.zip ZipEntry setTime

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

Introduction

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

Prototype

public void setTime(long time) 

Source Link

Document

Sets the last modification time of the entry.

Usage

From source file:org.dbgl.util.FileUtils.java

public static void zipEntry(final File orgFile, final File fileEntry, final ZipOutputStream zos)
        throws IOException {
    ZipEntry anEntry = new ZipEntry(PlatformUtils.toArchivePath(fileEntry, orgFile.isDirectory()));
    anEntry.setTime(orgFile.lastModified());
    if (orgFile.isFile() && !orgFile.canWrite())
        anEntry.setExtra(new byte[] { 1 });
    zos.putNextEntry(anEntry);//from w  ww.j a v a 2  s. c om

    if (orgFile.isFile()) {
        byte[] readBuffer = new byte[ZIP_BUFFER];
        int bytes = 0;
        FileInputStream is = new FileInputStream(orgFile);
        while ((bytes = is.read(readBuffer)) != -1)
            zos.write(readBuffer, 0, bytes);
        is.close();
    }
    zos.closeEntry();
}

From source file:edu.umd.cs.marmoset.modelClasses.ZipFileAggregator.java

private void addFileFromInputStream(String dirName, long time, InputStream inputStream) throws IOException {
    // Prepend directory name
    String name = dirName + "/nonzipfile";

    // Create output entry
    ZipEntry outputEntry = new ZipEntry(name);
    if (time > 0L)
        outputEntry.setTime(time); // only add valid times
    zipOutput.putNextEntry(outputEntry);

    // Copy zip input to output
    CopyUtils.copy(inputStream, zipOutput);

    zipOutput.closeEntry();/*w w  w. ja  va 2s .  com*/
}

From source file:org.apache.felix.deploymentadmin.itest.util.DPSigner.java

public void writeSignedManifest(Manifest manifest, ZipOutputStream zos, PrivateKey privKey,
        X509Certificate cert) throws Exception {
    zos.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME));
    manifest.write(zos);/*from w  w w. j  a  v a 2 s .c  o m*/
    zos.closeEntry();

    long now = System.currentTimeMillis();

    // Determine the signature-file manifest...
    Manifest sf = createSignatureFile(manifest);

    byte[] sfRawBytes;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        sf.write(baos);
        sfRawBytes = baos.toByteArray();
    }

    ZipEntry sigFileEntry = new ZipEntry(m_baseName.concat(".SF"));
    sigFileEntry.setTime(now);
    zos.putNextEntry(sigFileEntry);
    // Write the actual entry data...
    zos.write(sfRawBytes, 0, sfRawBytes.length);
    zos.closeEntry();

    // Create a PKCS#7 signature...
    byte[] encoded = calculateSignatureBlock(privKey, cert, sfRawBytes);

    ZipEntry blockFileEntry = new ZipEntry(m_baseName.concat(getBlockFileExtension(privKey)));
    blockFileEntry.setTime(now);
    zos.putNextEntry(blockFileEntry);
    zos.write(encoded);
    zos.closeEntry();
}

From source file:org.codice.ddf.configuration.migration.ExportMigrationContextImpl.java

@SuppressWarnings("PMD.DefaultPackage" /* designed to be called from ExportMigrationEntryImpl within this package */)
OutputStream getOutputStreamFor(ExportMigrationEntryImpl entry) {
    try {/*from   w ww  . j a  v  a  2 s .co m*/
        close();
        // zip entries are always Unix style based on our convention
        final ZipEntry ze = new ZipEntry(id + '/' + entry.getName());

        ze.setTime(entry.getLastModifiedTime()); // save the current modified time
        zipOutputStream.putNextEntry(ze);
        final OutputStream oos = new ProxyOutputStream(zipOutputStream) {
            @Override
            public void close() throws IOException {
                if (!(super.out instanceof ClosedOutputStream)) {
                    super.out = ClosedOutputStream.CLOSED_OUTPUT_STREAM;
                    zipOutputStream.closeEntry();
                }
            }

            @Override
            protected void handleIOException(IOException e) throws IOException {
                super.handleIOException(new ExportIOException(e));
            }
        };

        CipherOutputStream cos = cipherUtils.getCipherOutputStream(oos);
        this.currentOutputStream = cos;
        return cos;
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:com.ephesoft.dcma.util.FileUtils.java

/**
 * This method zips the contents of Directory specified into a zip file whose name is provided.
 * /*from w w  w .ja  v  a 2 s. com*/
 * @param dir2zip {@link String}
 * @param zout {@link String}
 * @param dir2zipName {@link String}
 * @throws IOException in case of error
 */
public static void zipDirectory(String dir2zip, ZipOutputStream zout, String dir2zipName) throws IOException {
    File srcDir = new File(dir2zip);
    List<String> fileList = listDirectory(srcDir);
    for (String fileName : fileList) {
        File file = new File(srcDir.getParent(), fileName);
        String zipName = fileName;
        if (File.separatorChar != FORWARD_SLASH) {
            zipName = fileName.replace(File.separatorChar, FORWARD_SLASH);
        }
        zipName = zipName.substring(
                zipName.indexOf(dir2zipName + BACKWARD_SLASH) + 1 + (dir2zipName + BACKWARD_SLASH).length());

        ZipEntry zipEntry;
        if (file.isFile()) {
            zipEntry = new ZipEntry(zipName);
            zipEntry.setTime(file.lastModified());
            zout.putNextEntry(zipEntry);
            FileInputStream fin = new FileInputStream(file);
            byte[] buffer = new byte[UtilConstants.BUFFER_CONST];
            for (int n; (n = fin.read(buffer)) > 0;) {
                zout.write(buffer, 0, n);
            }
            if (fin != null) {
                fin.close();
            }
        } else {
            zipEntry = new ZipEntry(zipName + FORWARD_SLASH);
            zipEntry.setTime(file.lastModified());
            zout.putNextEntry(zipEntry);
        }
    }
    if (zout != null) {
        zout.close();
    }
}

From source file:edu.umd.cs.marmoset.modelClasses.ZipFileAggregator.java

/**
 * Adds a zipfile from an inputStream to the aggregate zipfile.
 *
 * @param dirName name of the top-level directory that will be
 * @param inputStream the inputStream to the zipfile created in the aggregate zip file
 * @throws IOException/* www  .  j a v  a 2 s . c o  m*/
 * @throws BadInputZipFileException
 */
private void addZipFileFromInputStream(String dirName, long time, InputStream inputStream)
        throws IOException, BadInputZipFileException {
    // First pass: just scan through the contents of the
    // input file to make sure it's really valid.
    ZipInputStream zipInput = null;
    try {
        zipInput = new ZipInputStream(new BufferedInputStream(inputStream));
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            zipInput.closeEntry();
        }
    } catch (IOException e) {
        throw new BadInputZipFileException("Input zip file seems to be invalid", e);
    } finally {
        if (zipInput != null)
            zipInput.close();
    }

    // FIXME: It is probably wrong to call reset() on any input stream; for my application the inputStream will only ByteArrayInputStream or FileInputStream
    inputStream.reset();

    // Second pass: read each entry from the input zip file,
    // writing it to the output file.
    zipInput = null;
    try {
        // add the root directory with the correct timestamp
        if (time > 0L) {
            // Create output entry
            ZipEntry outputEntry = new ZipEntry(dirName + "/");
            outputEntry.setTime(time);
            zipOutput.closeEntry();
        }

        zipInput = new ZipInputStream(new BufferedInputStream(inputStream));
        ZipEntry entry;
        while ((entry = zipInput.getNextEntry()) != null) {
            try {
                String name = entry.getName();
                // Convert absolute paths to relative
                if (name.startsWith("/")) {
                    name = name.substring(1);
                }

                // Prepend directory name
                name = dirName + "/" + name;

                // Create output entry
                ZipEntry outputEntry = new ZipEntry(name);
                if (time > 0L)
                    outputEntry.setTime(time);
                zipOutput.putNextEntry(outputEntry);

                // Copy zip input to output
                CopyUtils.copy(zipInput, zipOutput);
            } catch (Exception zex) {
                // ignore it
            } finally {
                zipInput.closeEntry();
                zipOutput.closeEntry();
            }
        }
    } finally {
        if (zipInput != null) {
            try {
                zipInput.close();
            } catch (IOException ignore) {
                // Ignore
            }
        }
    }
}

From source file:org.apache.sling.reqanalyzer.impl.RequestAnalyzerWebConsole.java

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (req.getRequestURI().endsWith(RAW_FILE_MARKER) || req.getRequestURI().endsWith(ZIP_FILE_MARKER)) {

        InputStream input = null;
        OutputStream output = null;
        try {/*from   w ww  . j  a  v  a 2  s. c o m*/
            input = new FileInputStream(this.logFile);
            output = resp.getOutputStream();

            if (req.getRequestURI().endsWith(ZIP_FILE_MARKER)) {
                ZipOutputStream zip = new ZipOutputStream(output);
                zip.setLevel(Deflater.BEST_SPEED);

                ZipEntry entry = new ZipEntry(this.logFile.getName());
                entry.setTime(this.logFile.lastModified());
                entry.setMethod(ZipEntry.DEFLATED);

                zip.putNextEntry(entry);

                output = zip;
                resp.setContentType("application/zip");
            } else {
                resp.setContentType("text/plain");
                resp.setCharacterEncoding("UTF-8");
                resp.setHeader("Content-Length", String.valueOf(this.logFile.length())); // might be bigger than
            }
            resp.setDateHeader("Last-Modified", this.logFile.lastModified());

            IOUtils.copy(input, output);
        } catch (IOException ioe) {
            throw new ServletException("Cannot create copy of log file", ioe);
        } finally {
            IOUtils.closeQuietly(input);

            if (output instanceof ZipOutputStream) {
                ((ZipOutputStream) output).closeEntry();
                ((ZipOutputStream) output).finish();
            }
        }

        resp.flushBuffer();

    } else if (req.getRequestURI().endsWith(WINDOW_MARKER)) {

        if (canOpenSwingGui(req)) {
            showWindow();
        }

        String target = req.getRequestURI();
        target = target.substring(0, target.length() - WINDOW_MARKER.length());
        resp.sendRedirect(target);
        resp.flushBuffer();

    } else {

        super.service(req, resp);

    }
}

From source file:org.jumpmind.metl.core.runtime.component.Zip.java

@Override
public void handle(Message inputMessage, ISendMessageCallback messageTarget,
        boolean unitOfWorkBoundaryReached) {

    String targetPath = resolveParamsAndHeaders(targetRelativePath, inputMessage);
    if (inputMessage instanceof TextMessage) {
        List<String> files = ((TextMessage) inputMessage).getPayload();
        fileNames.addAll(files);//w  w w .j a  v  a2s.  co  m
        getComponentStatistics().incrementNumberEntitiesProcessed(files.size());
    }

    if (inputMessage instanceof ControlMessage) {
        IDirectory sourceDir = null;
        IDirectory targetDir = null;
        ZipOutputStream zos = null;

        sourceDir = sourceResource.reference();
        targetDir = targetResource.reference();

        try {
            targetDir.delete(targetPath);
            zos = new ZipOutputStream(targetDir.getOutputStream(targetPath, false), Charset.forName(encoding));

            for (String fileName : fileNames) {
                FileInfo sourceZipFile = sourceDir.listFile(fileName);
                log(LogLevel.INFO, "Received file name to add to zip: %s", sourceZipFile);
                if (mustExist && sourceZipFile == null) {
                    throw new IoException(String.format("Could not find file to zip: %s", sourceZipFile));
                }

                if (sourceZipFile != null) {
                    try {
                        if (!sourceZipFile.isDirectory()) {
                            ZipEntry entry = new ZipEntry(sourceZipFile.getName());
                            entry.setSize(sourceZipFile.getSize());
                            entry.setTime(sourceZipFile.getLastUpdated());
                            zos.putNextEntry(entry);
                            log(LogLevel.INFO, "Adding %s", sourceZipFile.getName());
                            InputStream fis = sourceDir.getInputStream(sourceZipFile.getRelativePath(),
                                    unitOfWorkBoundaryReached);
                            if (fis != null) {
                                try {
                                    IOUtils.copy(fis, zos);
                                } finally {
                                    IOUtils.closeQuietly(fis);
                                }
                            }
                        }
                        zos.closeEntry();
                    } catch (IOException e) {
                        throw new IoException(e);
                    }
                }
            }

            log(LogLevel.INFO, "Generated %s", targetPath);

        } finally {
            IOUtils.closeQuietly(zos);
        }

        if (deleteOnComplete) {
            for (String fileName : fileNames) {
                sourceDir.delete(fileName);
            }
        }

        fileNames.clear();
    }
}

From source file:com.taobao.android.builder.tools.asm.ClazzBasicHandler.java

protected void copyStreamToJar(InputStream zin, ZipOutputStream out, String currentName, long fileTime)
        throws IOException {
    // Create new entry for zip file.

    ZipEntry newEntry = new ZipEntry(currentName);
    // Make sure there is date and time set.
    if (fileTime != -1) {
        newEntry.setTime(fileTime); // If found set it into output file.
    }//  www .ja  v a 2s.com
    out.putNextEntry(newEntry);
    if (zin != null) {
        IOUtils.copy(zin, out);
    }
    IOUtils.closeQuietly(zin);

}

From source file:org.phpmaven.phpnar.PackageMojo.java

private void zipConfigPhpizeJs(ZipOutputStream zos, File file, String string) throws IOException {
    if (file.exists()) {
        zip(zos, file, string);/*from www  . j  ava  2  s.  co  m*/
    } else {
        // use a default file content taken from 5.3.10 for early 5.3.x versions (they did not contain the phpize windows variants but they should be compatible)
        final StringWriter writer = new StringWriter();
        IOUtils.copy(PackageMojo.class.getResourceAsStream("php5.3.x/config.phpize.js"), writer, "UTF-8");
        String contents = writer.toString();
        final ZipEntry entry = new ZipEntry(string.substring(1));
        entry.setTime(file.lastModified());
        zos.putNextEntry(entry);
        zos.write(contents.getBytes());
        zos.closeEntry();
    }
}