List of usage examples for java.util.zip ZipEntry setTime
public void setTime(long time)
From source file:de.xwic.appkit.core.util.ZipUtil.java
/** * Zips the files array into a file that has the name given as parameter. * /*from w w w. jav a 2 s .com*/ * @param files * the files array * @param zipFileName * the name for the zip file * @return the new zipped file * @throws IOException */ public static File zip(File[] files, String zipFileName) throws IOException { FileOutputStream stream = null; ZipOutputStream out = null; File archiveFile = null; try { if (!zipFileName.endsWith(".zip")) { zipFileName = zipFileName + ".zip"; } archiveFile = new File(zipFileName); byte buffer[] = new byte[BUFFER_SIZE]; // Open archive file stream = new FileOutputStream(archiveFile); out = new ZipOutputStream(stream); for (int i = 0; i < files.length; i++) { if (null == files[i] || !files[i].exists() || files[i].isDirectory()) { continue; } log.info("Zipping " + files[i].getName()); // Add archive entry ZipEntry zipAdd = new ZipEntry(files[i].getName()); zipAdd.setTime(files[i].lastModified()); out.putNextEntry(zipAdd); // Read input & write to output FileInputStream in = new FileInputStream(files[i]); while (true) { int nRead = in.read(buffer, 0, buffer.length); if (nRead <= 0) { break; } out.write(buffer, 0, nRead); } in.close(); } } catch (IOException e) { log.error("Error: " + e.getMessage(), e); throw e; } finally { try { if (null != out) { out.close(); } if (null != stream) { stream.close(); } } catch (IOException e) { log.error("Error: " + e.getMessage(), e); throw e; } } return archiveFile; }
From source file:com.isomorphic.maven.util.ArchiveUtils.java
/** * Steps common to archiving both zip and jar files, which include reading files from disk and using * their contents to create {@link ZipEntry ZipEntries} and writing them to a ZipOutputStream. * /*from w ww . j av a 2s . c o m*/ * @param root * @param source * @param target * @throws IOException */ private static void zip(File root, File source, ZipOutputStream target) throws IOException { String relativePath = root.toURI().relativize(source.toURI()).getPath().replace("\\", "/"); BufferedInputStream in = null; try { if (source.isDirectory()) { if (!relativePath.endsWith("/")) { relativePath += "/"; } ZipEntry entry = ZipEntryFactory.get(target, relativePath); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); for (File nestedFile : source.listFiles()) { zip(root, nestedFile, target); } return; } ZipEntry entry = ZipEntryFactory.get(target, relativePath); entry.setTime(source.lastModified()); target.putNextEntry(entry); in = new BufferedInputStream(FileUtils.openInputStream(source)); IOUtils.copy(in, target); target.closeEntry(); } finally { IOUtils.closeQuietly(in); } }
From source file:org.apache.kylin.common.util.ZipFileUtils.java
private static void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipOutputStream out) throws IOException { File[] files = new File(sourceDir).listFiles(); if (files == null) return;/*from w w w . ja v a2 s . c o m*/ for (File sourceFile : files) { if (sourceFile.isDirectory()) { compressDirectoryToZipfile(rootDir, sourceDir + normDir(sourceFile.getName()), out); } else { ZipEntry entry = new ZipEntry( normDir(StringUtils.isEmpty(rootDir) ? sourceDir : sourceDir.replace(rootDir, "")) + sourceFile.getName()); entry.setTime(sourceFile.lastModified()); out.putNextEntry(entry); FileInputStream in = new FileInputStream(sourceDir + sourceFile.getName()); try { IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); } } } }
From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java
private static int putEntries(ZipOutputStream zos, String sDir, String sRelativeDir, final List<File> excludedFiles) throws Exception { Engine.logEngine.trace("=========================================================="); Engine.logEngine.trace("sDir=" + sDir); Engine.logEngine.trace("sRelativeDir=" + sRelativeDir); Engine.logEngine.trace("excludedFiles=" + excludedFiles); File dir = new File(sDir); String[] files = dir.list(new FilenameFilter() { public boolean accept(File dir, String name) { File file = new File(dir, name); return (!excludedFiles.contains(file)); }//w w w . jav a2 s.com }); Engine.logEngine.trace("files=" + files); int nbe = 0; for (String file : files) { String sDirEntry = sDir + "/" + file; String sRelativeDirEntry = sRelativeDir != null ? (sRelativeDir + "/" + file) : file; File f = new File(sDirEntry); if (!f.isDirectory()) { Engine.logEngine.trace("+ " + sDirEntry); InputStream fi = new FileInputStream(f); try { ZipEntry entry = new ZipEntry(sRelativeDirEntry); entry.setTime(f.lastModified()); zos.putNextEntry(entry); IOUtils.copy(fi, zos); nbe++; } finally { fi.close(); } } else { nbe += putEntries(zos, sDirEntry, sRelativeDirEntry, excludedFiles); } } return nbe; }
From source file:org.sakaiproject.portal.charon.test.PortalTestFileUtils.java
private static void addSingleFile(String basePath, String replacePath, File file, ZipOutputStream zout, byte[] buffer) throws IOException { String path = file.getPath(); if (path.startsWith(basePath)) { path = replacePath + path.substring(basePath.length()); }//ww w. j av a 2s.com ZipEntry ze = new ZipEntry(path); ze.setTime(file.lastModified()); zout.putNextEntry(ze); try { InputStream fin = new FileInputStream(file); try { int len = 0; while ((len = fin.read(buffer)) > 0) { zout.write(buffer, 0, len); } } finally { fin.close(); } } finally { zout.closeEntry(); } }
From source file:JarUtils.java
/** * This recursive method writes all matching files and directories to * the jar output stream.//from w w w . jav a 2s .c o m */ private static void jar(File src, String prefix, JarInfo info) throws IOException { JarOutputStream jout = info.out; if (src.isDirectory()) { // create / init the zip entry prefix = prefix + src.getName() + "/"; ZipEntry entry = new ZipEntry(prefix); entry.setTime(src.lastModified()); entry.setMethod(JarOutputStream.STORED); entry.setSize(0L); entry.setCrc(0L); jout.putNextEntry(entry); jout.closeEntry(); // process the sub-directories File[] files = src.listFiles(info.filter); for (int i = 0; i < files.length; i++) { jar(files[i], prefix, info); } } else if (src.isFile()) { // get the required info objects byte[] buffer = info.buffer; // create / init the zip entry ZipEntry entry = new ZipEntry(prefix + src.getName()); entry.setTime(src.lastModified()); jout.putNextEntry(entry); // dump the file FileInputStream in = new FileInputStream(src); int len; while ((len = in.read(buffer, 0, buffer.length)) != -1) { jout.write(buffer, 0, len); } in.close(); jout.closeEntry(); } }
From source file:edu.umd.cs.submit.CommandLineSubmit.java
/** * @param p/*from w w w . j a v a 2s.co m*/ * @param find * @param files * @param userProps * @return * @throws IOException * @throws FileNotFoundException */ public static MultipartPostMethod createFilePost(Properties p, FindAllFiles find, Collection<File> files, Properties userProps) throws IOException, FileNotFoundException { // ========================== assemble zip file in byte array // ============================== String loginName = userProps.getProperty("loginName"); String classAccount = userProps.getProperty("classAccount"); String from = classAccount; if (loginName != null && !loginName.equals(classAccount)) from += "/" + loginName; System.out.println(" submitted by " + from); System.out.println(); System.out.println("Submitting the following files"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(4096); byte[] buf = new byte[4096]; ZipOutputStream zipfile = new ZipOutputStream(bytes); zipfile.setComment("zipfile for CommandLineTurnin, version " + VERSION); for (File resource : files) { if (resource.isDirectory()) continue; String relativePath = resource.getCanonicalPath().substring(find.rootPathLength + 1); System.out.println(relativePath); ZipEntry entry = new ZipEntry(relativePath); entry.setTime(resource.lastModified()); zipfile.putNextEntry(entry); InputStream in = new FileInputStream(resource); try { while (true) { int n = in.read(buf); if (n < 0) break; zipfile.write(buf, 0, n); } } finally { in.close(); } zipfile.closeEntry(); } // for each file zipfile.close(); MultipartPostMethod filePost = new MultipartPostMethod(p.getProperty("submitURL")); p.putAll(userProps); // add properties for (Map.Entry<?, ?> e : p.entrySet()) { String key = (String) e.getKey(); String value = (String) e.getValue(); if (!key.equals("submitURL")) filePost.addParameter(key, value); } filePost.addParameter("submitClientTool", "CommandLineTool"); filePost.addParameter("submitClientVersion", VERSION); byte[] allInput = bytes.toByteArray(); filePost.addPart(new FilePart("submittedFiles", new ByteArrayPartSource("submit.zip", allInput))); return filePost; }
From source file:com.diffplug.gradle.ZipMisc.java
/** * Modifies only the specified entries in a zip file. * * @param input a source from a zip file * @param output an output to a zip file * @param toModify a map from path to an input stream for the entries you'd like to change * @param toOmit a set of entries you'd like to leave out of the zip * @throws IOException/* www . j ava2 s . com*/ */ public static void modify(ByteSource input, ByteSink output, Map<String, Function<byte[], byte[]>> toModify, Predicate<String> toOmit) throws IOException { try (ZipInputStream zipInput = new ZipInputStream(input.openBufferedStream()); ZipOutputStream zipOutput = new ZipOutputStream(output.openBufferedStream())) { while (true) { // read the next entry ZipEntry entry = zipInput.getNextEntry(); if (entry == null) { break; } Function<byte[], byte[]> replacement = toModify.get(entry.getName()); if (replacement != null) { byte[] clean = ByteStreams.toByteArray(zipInput); byte[] modified = replacement.apply(clean); // if it's the entry being modified, enter the modified stuff try (InputStream replacementStream = new ByteArrayInputStream(modified)) { ZipEntry newEntry = new ZipEntry(entry.getName()); newEntry.setComment(entry.getComment()); newEntry.setExtra(entry.getExtra()); newEntry.setMethod(entry.getMethod()); newEntry.setTime(entry.getTime()); zipOutput.putNextEntry(newEntry); copy(replacementStream, zipOutput); } } else if (!toOmit.test(entry.getName())) { // if it isn't being modified, just copy the file stream straight-up ZipEntry newEntry = new ZipEntry(entry); newEntry.setCompressedSize(-1); zipOutput.putNextEntry(newEntry); copy(zipInput, zipOutput); } // close the entries zipInput.closeEntry(); zipOutput.closeEntry(); } } }
From source file:com.izforge.izpack.util.IoHelper.java
public static void copyStreamToJar(InputStream zin, java.util.zip.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. }/*from ww w . j a va2 s.c om*/ out.putNextEntry(newEntry); if (zin != null) { IOUtils.copy(zin, out); } out.closeEntry(); }
From source file:org.agnitas.util.ZipUtilities.java
/** * Add data to an open ZipOutputStream as a virtual file * //from w w w . j a v a 2 s. com * @param fileData * @param relativeDirPath * @param filename * @param destinationZipFileSream * @throws IOException */ public static void addFileDataToOpenZipFileStream(byte[] fileData, String relativeDirPath, String filename, ZipOutputStream destinationZipFileSream) throws IOException { if (fileData == null) throw new IOException("FileData is missing"); if (StringUtils.isEmpty(filename) || filename.trim().length() == 0) throw new IOException("Filename is missing"); if (destinationZipFileSream == null) throw new IOException("DestinationStream is not ready"); if (relativeDirPath == null || (!relativeDirPath.endsWith("/") && !relativeDirPath.endsWith("\\"))) throw new IOException("RelativeDirPath is invalid"); ZipEntry entry = new ZipEntry(relativeDirPath + filename); entry.setTime(new Date().getTime()); destinationZipFileSream.putNextEntry(entry); destinationZipFileSream.write(fileData); destinationZipFileSream.flush(); destinationZipFileSream.closeEntry(); }