Example usage for java.util.zip ZipEntry getTime

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

Introduction

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

Prototype

public long getTime() 

Source Link

Document

Returns the last modification time of the entry.

Usage

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testZipStuff() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    // name the file inside the zip  file
    final File oddFile = new File("src/test/resources/zip-timestamp/file-with-odd-time.txt");
    final File evenFile = new File("src/test/resources/zip-timestamp/file-with-even-time.txt");
    final ZipEntry oddZe = new ZipEntry(oddFile.getName());
    oddZe.setTime(oddFile.lastModified());
    zos.putNextEntry(oddZe);//from  w w  w  . j  a  v  a 2  s . c om
    final ZipEntry evenZe = new ZipEntry(evenFile.getName());
    evenZe.setTime(evenFile.lastModified());
    zos.putNextEntry(evenZe);
    zos.close();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ZipInputStream zipInputStream = new ZipInputStream(bais);
    final java.util.zip.ZipEntry oddEntry = zipInputStream.getNextEntry();
    System.out.println("oddEntry.getTime() = " + new Date(oddEntry.getTime()).toString());
    System.out.println("oddEntry.getName() = " + oddEntry.getName());
    final java.util.zip.ZipEntry evenEntry = zipInputStream.getNextEntry();
    System.out.println("evenEntry.getName() = " + evenEntry.getName());
    System.out.println("evenEntry.getTime() = " + new Date(evenEntry.getTime()).toString());
}

From source file:org.pentaho.reporting.libraries.repository.zip.ZipContentLocation.java

public ZipContentLocation(ZipRepository repository, ZipContentLocation parent, ZipEntry zipEntry) {
    if (repository == null) {
        throw new NullPointerException();
    }/*from  www  .  jav a 2s . c om*/
    if (parent == null) {
        throw new NullPointerException();
    }
    if (zipEntry == null) {
        throw new NullPointerException();
    }

    this.repository = repository;
    this.parent = parent;
    this.entryName = IOUtils.getInstance().getFileName(zipEntry.getName());
    this.comment = zipEntry.getComment();
    this.size = zipEntry.getSize();
    this.time = zipEntry.getTime();
    this.entries = new HashMap();
    this.name = RepositoryUtilities.buildName(this, "/") + '/';
}

From source file:it.greenvulcano.util.zip.ZipHelper.java

/**
 * Performs the uncompression of a <code>zip</code> file, whose name and
 * parent directory are passed as arguments, on the local filesystem. The
 * content of the <code>zip</code> file will be uncompressed within a
 * specified target directory.<br>
 *
 * @param srcDirectory//from  w  w w  .  j  ava  2s  . co m
 *            the source parent directory of the file/s to be unzipped. Must
 *            be an absolute pathname.
 * @param zipFilename
 *            the name of the file to be unzipped. Cannot contain wildcards.
 * @param targetDirectory
 *            the target directory in which the content of the
 *            <code>zip</code> file will be unzipped. Must be an absolute
 *            pathname.
 * @throws IOException
 *             If any error occurs during file uncompression.
 * @throws IllegalArgumentException
 *             if the arguments are invalid.
 */
public void unzipFile(String srcDirectory, String zipFilename, String targetDirectory) throws IOException {

    File srcDir = new File(srcDirectory);

    if (!srcDir.isAbsolute()) {
        throw new IllegalArgumentException(
                "The pathname of the source parent directory is NOT absolute: " + srcDirectory);
    }

    if (!srcDir.exists()) {
        throw new IllegalArgumentException(
                "Source parent directory " + srcDirectory + " NOT found on local filesystem.");
    }

    if (!srcDir.isDirectory()) {
        throw new IllegalArgumentException("Source parent directory " + srcDirectory + " is NOT a directory.");
    }

    File srcZipFile = new File(srcDirectory, zipFilename);
    if (!srcZipFile.exists()) {
        throw new IllegalArgumentException(
                "File to be unzipped (" + srcZipFile.getAbsolutePath() + ") NOT found on local filesystem.");
    }

    ZipFile zipFile = null;
    try {
        zipFile = new ZipFile(srcZipFile);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry currEntry = entries.nextElement();
            if (currEntry.isDirectory()) {
                String targetSubdirPathname = currEntry.getName();
                File dir = new File(targetDirectory, targetSubdirPathname);
                FileUtils.forceMkdir(dir);
                dir.setLastModified(currEntry.getTime());
            } else {
                InputStream is = null;
                OutputStream os = null;
                File file = null;
                try {
                    is = zipFile.getInputStream(currEntry);
                    FileUtils.forceMkdir(new File(targetDirectory, currEntry.getName()).getParentFile());
                    file = new File(targetDirectory, currEntry.getName());
                    os = new FileOutputStream(file);
                    IOUtils.copy(is, os);
                } finally {
                    try {
                        if (is != null) {
                            is.close();
                        }

                        if (os != null) {
                            os.close();
                        }
                    } catch (IOException exc) {
                        // Do nothing
                    }

                    if (file != null) {
                        file.setLastModified(currEntry.getTime());
                    }
                }
            }
        }
    } finally {
        try {
            if (zipFile != null) {
                zipFile.close();
            }
        } catch (Exception exc) {
            // do nothing
        }
    }
}

From source file:ZipFileViewer.java

public Object getValueAt(int row, int col) {
    ZipEntry zipEntry = (ZipEntry) (m_zipEntries.get(row));
    switch (col) {
    case NAME://from w w  w .j  av  a  2 s . c  o  m
        return zipEntry.getName();
    case SIZE:
        if (zipEntry.isDirectory()) {
            return "";
        } else {
            return String.valueOf(zipEntry.getSize() / 1000) + " KB";
        }
    case COMP_SIZE:
        if (zipEntry.isDirectory()) {
            return "";
        } else {
            return String.valueOf(zipEntry.getCompressedSize() / 1000) + " KB";
        }
    case TYPE:
        if (zipEntry.isDirectory()) {
            return "Directory";
        } else {
            return "File";
        }
    case LAST_MODI:
        return String.valueOf(new Date(zipEntry.getTime()));
    }
    return new String();
}

From source file:com.dbi.jmmerge.MapController.java

private Map<String, File> extractFilesFromZipUpload(MultipartFile file) throws IOException {
    Map<String, File> ret = new HashMap<>();
    String baseDirName = null;/*from  w w  w .  j av  a 2s . c  o  m*/
    //First catalog it, to see what we've got.
    File temp = File.createTempFile("map", null);
    temp.deleteOnExit();
    file.transferTo(temp);
    ZipInputStream zipin = new ZipInputStream(new FileInputStream(temp));
    ZipEntry entry = zipin.getNextEntry();
    byte[] buf = new byte[1024];
    do {
        FileOutputStream out = null;
        String filename = entry.getName();
        if (isJunkEntry(entry.getName()) || entry.isDirectory()) {
            continue;
        }
        try {
            ret.put(filename, File.createTempFile(filename, null));
            LOG.debug("Incoming timestamp on zip - " + filename + " - " + entry.getTime());
            ret.get(filename).deleteOnExit();
            out = new FileOutputStream(ret.get(filename));
            IOUtils.copy(zipin, out);
            ret.get(filename).setLastModified(entry.getTime());
        } finally {
            // we must always close the output file
            if (out != null)
                out.close();
        }
    } while ((entry = zipin.getNextEntry()) != null);
    baseDirName = tryToGuessBaseDir(ret.keySet());
    if (baseDirName != null) {
        for (String key : new HashSet<String>(ret.keySet())) {
            ret.put(key.replace(baseDirName + "/", ""), ret.remove(key));
        }
    }
    return ret;
}

From source file:org.codehaus.plexus.archiver.zip.ZipArchiverTest.java

public void testLookAtExtraZipFields_from_macos() throws IOException {
    InputStream fis = Streams.fileInputStream(new File("src/test/resources/zip-timestamp/macOsZipFile.zip"));
    ZipInputStream zis = new ZipInputStream(fis);
    final java.util.zip.ZipEntry evenEntry = zis.getNextEntry();
    final ZipExtraField[] parse = ExtraFieldUtils.parse(evenEntry.getExtra());
    System.out.println(Arrays.asList(parse));
    final java.util.zip.ZipEntry oddEntry = zis.getNextEntry();

    System.out.println(Arrays.asList(ExtraFieldUtils.parse(oddEntry.getExtra())));

    System.out.println("oddEntry.getTime() = " + new Date(oddEntry.getTime()).toString());
    System.out.println("oddEntry.getName() = " + oddEntry.getName());
    System.out.println("new String(oddEntry.getExtra()) = " + new String(oddEntry.getExtra()));
    System.out.println("evenEntry.getName() = " + evenEntry.getName());
    System.out.println("evenEntry.getTime() = " + new Date(evenEntry.getTime()).toString());
    System.out.println("new String(evenEntry.getExtra()) = " + new String(evenEntry.getExtra()));

}

From source file:org.zeroturnaround.zip.ZipsTest.java

public void testPreservingTimestamps() throws IOException {
    File src = new File(MainExamplesTest.DEMO_ZIP);

    File dest = File.createTempFile("temp", ".zip");
    final ZipFile zf = new ZipFile(src);
    try {//from   w w  w. j  av a2s .co m
        Zips.get(src).addEntries(new ZipEntrySource[0]).preserveTimestamps().destination(dest).process();
        Zips.get(dest).iterate(new ZipEntryCallback() {
            public void process(InputStream in, ZipEntry zipEntry) throws IOException {
                String name = zipEntry.getName();
                assertEquals("Timestapms differ at entry " + name, zf.getEntry(name).getTime(),
                        zipEntry.getTime());
            }
        });
    } finally {
        ZipUtil.closeQuietly(zf);
        FileUtils.deleteQuietly(dest);
    }
}

From source file:org.zeroturnaround.zip.ZipsTest.java

public void testPreservingTimestampsSetter() throws IOException {
    File src = new File(MainExamplesTest.DEMO_ZIP);

    File dest = File.createTempFile("temp", ".zip");
    final ZipFile zf = new ZipFile(src);
    try {/*w  w  w  .j a v a2  s .c o m*/
        Zips.get(src).addEntries(new ZipEntrySource[0]).setPreserveTimestamps(true).destination(dest).process();
        Zips.get(dest).iterate(new ZipEntryCallback() {
            public void process(InputStream in, ZipEntry zipEntry) throws IOException {
                String name = zipEntry.getName();
                assertEquals("Timestapms differ at entry " + name, zf.getEntry(name).getTime(),
                        zipEntry.getTime());
            }
        });
    } finally {
        ZipUtil.closeQuietly(zf);
        FileUtils.deleteQuietly(dest);
    }
}

From source file:org.rapidcontext.core.storage.ZipStorage.java

/**
 * Initializes this object. This method locates all the ZIP file
 * entries and creates all the indexes./*from  w ww .  j  a  va  2  s  . c  o m*/
 */
public void init() {
    Index idx = new Index(Path.ROOT);
    idx.addObject(PATH_STORAGEINFO.name());
    idx.updateLastModified(new Date(file().lastModified()));
    entries.put(Path.ROOT, idx);
    Enumeration e = zip.entries();
    while (e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        String name = entry.getName();
        if (entry.isDirectory() && !name.endsWith("/")) {
            name += "/";
        } else {
            name = StringUtils.removeEnd(name, DirStorage.SUFFIX_PROPS);
        }
        Path path = new Path(name);
        Path parent = path.parent();
        idx = (Index) entries.get(parent);
        if (path.isIndex()) {
            idx.addIndex(path.name());
            idx = new Index(path);
            idx.updateLastModified(new Date(entry.getTime()));
            entries.put(path, idx);
        } else {
            idx.addObject(path.name());
            entries.put(path, entry);
        }
    }
}

From source file:com.facebook.buck.util.zip.ZipScrubberTest.java

@Test
public void modificationTimesExceedShort() throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] data = "data1".getBytes(Charsets.UTF_8);
    try (ZipOutputStream out = new ZipOutputStream(byteArrayOutputStream)) {
        for (long i = 0; i < Short.MAX_VALUE + 1; i++) {
            ZipEntry entry = new ZipEntry("file" + i);
            entry.setSize(data.length);/*w  ww  . java 2  s .c  o m*/
            out.putNextEntry(entry);
            out.write(data);
            out.closeEntry();
        }
    }

    byte[] bytes = byteArrayOutputStream.toByteArray();
    ZipScrubber.scrubZipBuffer(bytes.length, ByteBuffer.wrap(bytes));

    // Iterate over each of the entries, expecting to see all zeros in the time fields.
    Date dosEpoch = new Date(ZipUtil.dosToJavaTime(ZipConstants.DOS_FAKE_TIME));
    try (ZipInputStream is = new ZipInputStream(new ByteArrayInputStream(bytes))) {
        for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
            assertThat(entry.getName(), new Date(entry.getTime()), Matchers.equalTo(dosEpoch));
        }
    }
}