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:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/* w w w  .  ja  v  a2 s  . c  o m*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setTime(new Date().getTime());
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:org.celeria.minecraft.backup.Archive.java

private ZipEntry entryFor(final String name, final FileContent content) throws FileSystemException {
    final ZipEntry entry = new ZipEntry(name);
    entry.setTime(content.getLastModifiedTime());
    return entry;
}

From source file:de.jwi.zip.Zipper.java

public void zip(ZipOutputStream out, File f, File base) throws IOException {
    String name = f.getPath().replace('\\', '/');

    if (base != null) {
        String basename = base.getPath().replace('\\', '/');

        if (name.startsWith(basename)) {
            name = name.substring(basename.length());
        }/* w ww  .ja va 2s . c  o m*/
    }

    if (name.startsWith("/")) {
        name = name.substring(1);
    }

    ZipEntry entry = new ZipEntry(name);
    entry.setTime(f.lastModified());

    out.putNextEntry(entry);

    FileInputStream is = new FileInputStream(f);

    byte[] buf = new byte[BUFSIZE];

    try {
        int l;
        while ((l = is.read(buf)) > -1) {
            out.write(buf, 0, l);
        }
    } finally {
        is.close();
    }
    out.closeEntry();

}

From source file:org.structr.core.graph.SyncCommand.java

private static void exportDirectory(ZipOutputStream zos, File dir, String path, Set<String> filesToInclude)
        throws IOException {

    final String nestedPath = path + dir.getName() + "/";
    final ZipEntry dirEntry = new ZipEntry(nestedPath);
    zos.putNextEntry(dirEntry);//  ww  w .j  a  v a 2s .  c  o m

    final File[] contents = dir.listFiles();
    if (contents != null) {

        for (File file : contents) {

            if (file.isDirectory()) {

                exportDirectory(zos, file, nestedPath, filesToInclude);

            } else {

                final String fileName = file.getName();
                final String relativePath = nestedPath + fileName;
                boolean includeFile = true;

                if (filesToInclude != null) {

                    includeFile = false;

                    if (filesToInclude.contains(fileName)) {

                        includeFile = true;
                    }
                }

                if (includeFile) {

                    // create ZIP entry
                    ZipEntry fileEntry = new ZipEntry(relativePath);
                    fileEntry.setTime(file.lastModified());
                    zos.putNextEntry(fileEntry);

                    // copy file into stream
                    FileInputStream fis = new FileInputStream(file);
                    IOUtils.copy(fis, zos);
                    fis.close();

                    // flush and close entry
                    zos.flush();
                    zos.closeEntry();
                }
            }
        }
    }

    zos.closeEntry();

}

From source file:com.jaxio.celerio.output.ZipOutputResult.java

@Override
public void addContent(InputStream contentStream, String entryName, TemplatePack templatePack,
        Template template) throws IOException {
    open();//w  w  w .j  a  v a  2  s .c  om

    if (fileList.contains(entryName)) {
        log.error("Ignore duplicate entry: " + entryName);
        return;
    } else {
        fileList.add(entryName);
    }

    // Add archive entry
    ZipEntry zipEntry = new JarEntry(normalize(entryName));
    zipEntry.setTime((new Date()).getTime());
    zipOutputStream.putNextEntry(zipEntry);

    IOUtils.copy(contentStream, zipOutputStream);
}

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

@VisibleForTesting
void copyToOutputZipFile(ZipEntry entry) {
    InputStream is = null;//from w  w w.j  ava 2s .  c om

    try {
        final ZipEntry ze = new ZipEntry(entry.getName());

        ze.setTime(entry.getTime());
        zipOutputStream.putNextEntry(ze);
        if (!entry.isDirectory()) {
            is = zip.getInputStream(entry);
            IOUtils.copy(is, zipOutputStream);
        }
    } catch (IOException e) {
        getReport().record(new MigrationException(Messages.DECRYPT_PATH_ERROR, entry.getName(), e));
    } finally {
        IOUtils.closeQuietly(is); // we do not care about failing to close this stream
    }
}

From source file:org.tangram.components.CodeExporter.java

@LinkAction("/codes.zip")
public TargetDescriptor codes(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (!request.getRequestURI().endsWith(".zip")) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return null;
    } // if/*from  ww w .ja v  a  2 s .co m*/
    if (request.getAttribute(Constants.ATTRIBUTE_ADMIN_USER) == null) {
        throw new IOException("User may not execute action");
    } // if

    long now = System.currentTimeMillis();

    response.setContentType("application/x-zip-compressed");

    CRC32 crc = new CRC32();

    ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
    zos.setComment("Tangram Repository Codes");
    zos.setLevel(9);
    Collection<CodeResource> codes = codeResourceCache.getCodes();
    for (CodeResource code : codes) {
        if (StringUtils.isNotBlank(code.getAnnotation())) {
            String mimeType = CodeHelper.getNormalizedMimeType(code.getMimeType());
            String folder = CodeHelper.getFolder(mimeType);
            String extension = CodeHelper.getExtension(mimeType);
            if (mimeType.startsWith("text/")) {
                byte[] bytes = code.getCodeText().getBytes("UTF-8");
                ZipEntry ze = new ZipEntry(folder + "/" + getFilename(code) + extension);
                ze.setTime(now);
                crc.reset();
                crc.update(bytes);
                ze.setCrc(crc.getValue());
                zos.putNextEntry(ze);
                zos.write(bytes);
                zos.closeEntry();
            } // if
        } // if
    } // for
    zos.finish();
    zos.close();

    return TargetDescriptor.DONE;
}

From source file:com.thoughtworks.go.util.ZipUtil.java

void addToZip(ZipPath path, File srcFile, ZipOutputStream zip, boolean excludeRootDir) throws IOException {
    if (srcFile.isDirectory()) {
        addFolderToZip(path, srcFile, zip, excludeRootDir);
    } else {/*from   w  ww .  j  ava 2s.c  o  m*/
        byte[] buff = new byte[4096];
        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(srcFile))) {
            ZipEntry zipEntry = path.with(srcFile).asZipEntry();
            zipEntry.setTime(srcFile.lastModified());
            zip.putNextEntry(zipEntry);
            int len;
            while ((len = inputStream.read(buff)) > 0) {
                zip.write(buff, 0, len);
            }
        }
    }
}

From source file:org.junitee.testngee.servlet.TestNGEEServlet.java

private void zipOutputDir(File outdir, ServletOutputStream outputStream) throws IOException {
    ZipOutputStream out = new ZipOutputStream(outputStream);
    File[] files = outdir.listFiles();

    out.setMethod(ZipOutputStream.DEFLATED);

    for (int i = 0; i < files.length; i++) {
        File file = files[i];/*from  w w w .  j ava2  s  . c o m*/

        if (!file.isDirectory()) {
            ZipEntry zipEntry = new ZipEntry(file.getName());
            zipEntry.setSize(file.length());
            zipEntry.setTime(file.lastModified());

            out.putNextEntry(zipEntry);

            FileInputStream in = new FileInputStream(file);
            byte[] buffer = new byte[4096];
            int r;

            while ((r = in.read(buffer)) > 0) {
                out.write(buffer, 0, r);
            }
            in.close();
            out.closeEntry();
        }
    }
    out.close();
}

From source file:com.opensymphony.xwork2.util.fs.JarEntryRevisionTest.java

private String createJarFile(long time) throws Exception {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    Path jarPath = Paths/* w  ww . j av  a 2 s.  c o  m*/
            .get(Thread.currentThread().getContextClassLoader().getResource("xwork-jar.jar").toURI())
            .getParent();
    File jarFile = jarPath.resolve("JarEntryRevisionTest_testNeedsReloading.jar").toFile();
    FileOutputStream fos = new FileOutputStream(jarFile, false);
    JarOutputStream target = new JarOutputStream(fos, manifest);
    target.putNextEntry(new ZipEntry("com/opensymphony/xwork2/util/fs/"));
    ZipEntry entry = new ZipEntry("com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class");
    entry.setTime(time);
    target.putNextEntry(entry);
    InputStream source = getClass()
            .getResourceAsStream("/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class");
    IOUtils.copy(source, target);
    source.close();
    target.closeEntry();
    target.close();
    fos.close();

    return jarFile.toURI().toURL().toExternalForm();
}