List of usage examples for java.util.zip ZipEntry getTime
public long getTime()
From source file:org.structr.core.graph.SyncCommand.java
private static void importDirectory(ZipInputStream zis, ZipEntry entry) throws IOException { if (entry.isDirectory()) { File newDir = new File(entry.getName()); if (!newDir.exists()) { newDir.mkdirs();//from w ww. j a v a2 s . c om } } else { File newFile = new File(entry.getName()); boolean overwrite = false; if (!newFile.exists()) { overwrite = true; } else { if (newFile.lastModified() < entry.getTime()) { logger.log(Level.INFO, "Overwriting existing file {0} because import file is newer.", entry.getName()); overwrite = true; } } if (overwrite) { FileOutputStream fos = new FileOutputStream(newFile); IOUtils.copy(zis, fos); fos.flush(); fos.close(); } } }
From source file:com.haulmont.cuba.web.controllers.StaticContentController.java
protected LookupResult lookupNoCache(HttpServletRequest req) { final String path = getPath(req); if (isForbidden(path)) return new Error(HttpServletResponse.SC_FORBIDDEN, "Forbidden"); ServletContext context = req.getSession().getServletContext(); final URL url; try {/*from w w w . j a v a 2s. c o m*/ url = context.getResource(path); } catch (MalformedURLException e) { return new Error(HttpServletResponse.SC_BAD_REQUEST, "Malformed path"); } if (url == null) return new Error(HttpServletResponse.SC_NOT_FOUND, "Not found"); final String mimeType = getMimeType(path); final String realpath = context.getRealPath(path); if (realpath != null) { // Try as an ordinary file File f = new File(realpath); if (!f.isFile()) return new Error(HttpServletResponse.SC_FORBIDDEN, "Forbidden"); else { return createLookupResult(req, f.lastModified(), mimeType, (int) f.length(), acceptsDeflate(req), url); } } else { try { // Try as a JAR Entry final ZipEntry ze = ((JarURLConnection) url.openConnection()).getJarEntry(); if (ze != null) { if (ze.isDirectory()) return new Error(HttpServletResponse.SC_FORBIDDEN, "Forbidden"); else return createLookupResult(req, ze.getTime(), mimeType, (int) ze.getSize(), acceptsDeflate(req), url); } else // Unexpected? return new StaticFile(-1, mimeType, -1, acceptsDeflate(req), url); } catch (ClassCastException e) { // Unknown resource type return createLookupResult(req, -1, mimeType, -1, acceptsDeflate(req), url); } catch (IOException e) { return new Error(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error"); } } }
From source file:org.dspace.webmvc.controller.ResourceController.java
protected LookupResult lookupNoCache(HttpServletRequest req) { final String path = getPath(req); if (isForbidden(path)) { return new Error(HttpServletResponse.SC_FORBIDDEN, "Forbidden"); }/* w w w . j av a2 s. c o m*/ final URL url; try { url = req.getSession().getServletContext().getResource(path); } catch (MalformedURLException e) { return new Error(HttpServletResponse.SC_BAD_REQUEST, "Malformed path"); } final String mimeType = getMimeType(req, path); final String realpath = req.getSession().getServletContext().getRealPath(path); if (url != null && realpath != null) { // Try as an ordinary file File f = new File(realpath); if (!f.isFile()) { return new Error(HttpServletResponse.SC_FORBIDDEN, "Forbidden"); } else { return new StaticFile(f.lastModified(), mimeType, (int) f.length(), acceptsDeflate(req), url); } } else { ClassPathResource cpr = new ClassPathResource(path); if (cpr.exists()) { URL cprURL = null; try { cprURL = cpr.getURL(); // Try as a JAR Entry final ZipEntry ze = ((JarURLConnection) cprURL.openConnection()).getJarEntry(); if (ze != null) { if (ze.isDirectory()) { return new Error(HttpServletResponse.SC_FORBIDDEN, "Forbidden"); } else { return new StaticFile(ze.getTime(), mimeType, (int) ze.getSize(), acceptsDeflate(req), cprURL); } } else { // Unexpected? return new StaticFile(-1, mimeType, -1, acceptsDeflate(req), cprURL); } } catch (ClassCastException e) { // Unknown resource type if (url != null) { return new StaticFile(-1, mimeType, -1, acceptsDeflate(req), cprURL); } else { return new Error(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error"); } } catch (IOException e) { return new Error(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error"); } } else { return new Error(HttpServletResponse.SC_NOT_FOUND, "Not found"); } } }
From source file:org.codice.ddf.configuration.migration.ImportMigrationEntryImplTest.java
@Test public void getLastModifiedTime() { givenMockedPathUtils();/*from w ww . j a va2 s . c o m*/ final java.util.zip.ZipEntry mockZipEntry = mock(java.util.zip.ZipEntry.class); when(mockZipEntry.getTime()).thenReturn(10L); when(mockZipEntry.getName()).thenReturn(ENTRY_NAME); final ImportMigrationEntryImpl entry = new ImportMigrationEntryImpl(getContextFunction, mockZipEntry); assertThat(entry.getLastModifiedTime(), equalTo(10L)); }
From source file:org.apache.sling.jcr.contentloader.internal.readers.ZipReader.java
/** * @see org.apache.sling.jcr.contentloader.ContentReader#parse(java.io.InputStream, org.apache.sling.jcr.contentloader.ContentCreator) *//*from w ww .j a v a2s . co m*/ public void parse(InputStream ins, ContentCreator creator) throws IOException, RepositoryException { try { creator.createNode(null, NT_FOLDER, null); final ZipInputStream zis = new ZipInputStream(ins); ZipEntry entry; do { entry = zis.getNextEntry(); if (entry != null) { if (!entry.isDirectory()) { String name = entry.getName(); int pos = name.lastIndexOf('/'); if (pos != -1) { creator.switchCurrentNode(name.substring(0, pos), NT_FOLDER); } creator.createFileAndResourceNode(name, new CloseShieldInputStream(zis), null, entry.getTime()); creator.finishNode(); creator.finishNode(); if (pos != -1) { creator.finishNode(); } } zis.closeEntry(); } } while (entry != null); creator.finishNode(); } finally { if (ins != null) { try { ins.close(); } catch (IOException ignore) { } } } }
From source file:org.pentaho.reporting.libraries.repository.zipreader.ZipReadContentLocation.java
public ZipReadContentLocation(ZipReadRepository repository, ZipReadContentLocation parent, ZipEntry zipEntry) { if (repository == null) { throw new NullPointerException(); }/*from w ww .j a v a 2 s . co m*/ if (parent == null) { throw new NullPointerException(); } if (zipEntry == null) { throw new NullPointerException(); } this.repository = repository; this.parent = parent; this.comment = zipEntry.getComment(); this.size = zipEntry.getSize(); this.time = zipEntry.getTime(); this.entries = new HashMap(); this.entryName = IOUtils.getInstance().getFileName(zipEntry.getName()); this.name = RepositoryUtilities.buildName(this, "/") + '/'; }
From source file:org.atombeat.xquery.functions.util.GetZipEntries.java
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {/*from ww w . j a v a2s . c o m*/ String path = args[0].getStringValue(); ZipFile zf = new ZipFile(path); log.debug(zf.getName()); log.debug(zf.size()); log.debug(zf.hashCode()); ZipEntry e; Enumeration<? extends ZipEntry> entries = zf.entries(); ValueSequence s = new ValueSequence(); for (int i = 0; entries.hasMoreElements(); i++) { log.debug(i); e = entries.nextElement(); log.debug(e.getName()); log.debug(e.getComment()); log.debug(e.isDirectory()); log.debug(e.getCompressedSize()); log.debug(e.getCrc()); log.debug(e.getMethod()); log.debug(e.getSize()); log.debug(e.getTime()); if (!e.isDirectory()) s.add(new StringValue(e.getName())); } return s; } catch (Exception e) { throw new XPathException("error processing zip file: " + e.getLocalizedMessage(), e); } }
From source file:JarUtils.java
public static void unjar(InputStream in, File dest) throws IOException { if (!dest.exists()) { dest.mkdirs();/*from w w w . j av a 2 s. c o m*/ } if (!dest.isDirectory()) { throw new IOException("Destination must be a directory."); } JarInputStream jin = new JarInputStream(in); byte[] buffer = new byte[1024]; ZipEntry entry = jin.getNextEntry(); while (entry != null) { String fileName = entry.getName(); if (fileName.charAt(fileName.length() - 1) == '/') { fileName = fileName.substring(0, fileName.length() - 1); } if (fileName.charAt(0) == '/') { fileName = fileName.substring(1); } if (File.separatorChar != '/') { fileName = fileName.replace('/', File.separatorChar); } File file = new File(dest, fileName); if (entry.isDirectory()) { // make sure the directory exists file.mkdirs(); jin.closeEntry(); } else { // make sure the directory exists File parent = file.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } // dump the file OutputStream out = new FileOutputStream(file); int len = 0; while ((len = jin.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); out.close(); jin.closeEntry(); file.setLastModified(entry.getTime()); } entry = jin.getNextEntry(); } /* Explicity write out the META-INF/MANIFEST.MF so that any headers such as the Class-Path are see for the unpackaged jar */ Manifest mf = jin.getManifest(); if (mf != null) { File file = new File(dest, "META-INF/MANIFEST.MF"); File parent = file.getParentFile(); if (parent.exists() == false) { parent.mkdirs(); } OutputStream out = new FileOutputStream(file); mf.write(out); out.flush(); out.close(); } jin.close(); }
From source file:org.apache.webdav.ant.taskdefs.Put.java
private void uploadZipEntry(HttpURL url, String name, ZipFile zipFile) throws IOException { boolean putit = false; ZipEntry entry = zipFile.getEntry(name); try {//from w w w .ja va 2s. c om if (this.overwrite) { putit = true; } else { // check last modified date (both GMT) long remoteLastMod = Utils.getLastModified(getHttpClient(), url); long localLastMod = entry.getTime(); putit = localLastMod > remoteLastMod; } } catch (HttpException e) { switch (e.getReasonCode()) { case HttpStatus.SC_NOT_FOUND: putit = true; break; default: throw Utils.makeBuildException("Can't get lastmodified!?", e); } } if (putit) { log("Uploading: " + name, ifVerbose()); String contentType = Mimetypes.getMimeType(name, DEFAULT_CONTENT_TYPE); Utils.putFile(getHttpClient(), url, zipFile.getInputStream(entry), contentType, this.locktoken); this.countWrittenFiles++; } else { countOmittedFiles++; log("Omitted: " + name + " (uptodate)", ifVerbose()); } }
From source file:slash.navigation.download.tools.UpdateCatalog.java
private void updateMap(DatasourceType datasourceType, Map map) throws IOException { String url = datasourceType.getBaseUrl() + map.getUri(); // HEAD for last modified, content length, etag Download download = head(url);/*from ww w . ja v a 2s. com*/ if (download.getState().equals(NotModified)) return; if (download.getState().equals(Failed)) { log.severe(format("Failed to download %s as a map", map.getUri())); return; } Checksum checksum = download.getFile().getActualChecksum(); MapType mapType = createMapType(map.getUri(), singletonList(checksum), null); datasourceType.getMap().add(mapType); // GET with range for .zip or .map header if (!download.getFile().getFile().exists()) download = downloadPartial(url, checksum.getContentLength()); if (download.getState().equals(Failed)) { log.severe(format("Failed to download %s partially as a map", map.getUri())); return; } if (map.getUri().endsWith(DOT_ZIP)) { try (ZipInputStream zipInputStream = new ZipInputStream( new FileInputStream(download.getFile().getFile()))) { ZipEntry entry = zipInputStream.getNextEntry(); while (entry != null) { if (!entry.isDirectory() && entry.getName().endsWith(DOT_MAP)) { log.info(format("Found map %s in URI %s", entry.getName(), map.getUri())); mapType.getFragment() .add(createFragmentType(entry.getName(), entry.getTime(), entry.getSize())); BoundingBox boundingBox = MapUtil.extractBoundingBox(zipInputStream, entry.getSize()); if (boundingBox != null) mapType.setBoundingBox(asBoundingBoxType(boundingBox)); // do not close zip input stream and cope with partially copied zips try { zipInputStream.closeEntry(); } catch (EOFException e) { // intentionally left empty } } try { entry = zipInputStream.getNextEntry(); } catch (EOFException e) { entry = null; } } } } else if (map.getUri().endsWith(DOT_MAP)) { log.info(format("Found map %s", map.getUri())); BoundingBox boundingBox = MapUtil.extractBoundingBox(download.getFile().getFile()); if (boundingBox != null) mapType.setBoundingBox(asBoundingBoxType(boundingBox)); } else log.warning(format("Ignoring %s as a map", map.getUri())); updatePartially(datasourceType); }