List of usage examples for java.util.zip ZipEntry setTime
public void setTime(long time)
From source file:com.taobao.android.builder.tools.classinject.CodeInjectByJavassist.java
private static void copyStreamToJar(InputStream zin, ZipOutputStream out, String currentName, long fileTime, ZipEntry zipEntry) 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. }// w w w. jav a 2 s. c o m out.putNextEntry(newEntry); if (zin != null) { IOUtils.copy(zin, out); } }
From source file:com.datasalt.pangool.solr.SolrRecordWriter.java
/** * Write a file to a zip output stream, removing leading path name components from the actual file name when creating * the zip file entry./* ww w. jav a 2s. c o m*/ * * The entry placed in the zip file is <code>baseName</code>/ <code>relativePath</code>, where * <code>relativePath</code> is constructed by removing a leading <code>root</code> from the path for * <code>itemToZip</code>. * * If <code>itemToZip</code> is an empty directory, it is ignored. If <code>itemToZip</code> is a directory, the * contents of the directory are added recursively. * * @param zos * The zip output stream * @param baseName * The base name to use for the file name entry in the zip file * @param root * The path to remove from <code>itemToZip</code> to make a relative path name * @param itemToZip * The path to the file to be added to the zip file * @return the number of entries added * @throws IOException */ static public int zipDirectory(final Configuration conf, final ZipOutputStream zos, final String baseName, final String root, final Path itemToZip) throws IOException { LOG.info(String.format("zipDirectory: %s %s %s", baseName, root, itemToZip)); LocalFileSystem localFs = FileSystem.getLocal(conf); int count = 0; final FileStatus itemStatus = localFs.getFileStatus(itemToZip); if (itemStatus.isDir()) { final FileStatus[] statai = localFs.listStatus(itemToZip); // Add a directory entry to the zip file final String zipDirName = relativePathForZipEntry(itemToZip.toUri().getPath(), baseName, root); final ZipEntry dirZipEntry = new ZipEntry(zipDirName + Path.SEPARATOR_CHAR); LOG.info(String.format("Adding directory %s to zip", zipDirName)); zos.putNextEntry(dirZipEntry); zos.closeEntry(); count++; if (statai == null || statai.length == 0) { LOG.info(String.format("Skipping empty directory %s", itemToZip)); return count; } for (FileStatus status : statai) { count += zipDirectory(conf, zos, baseName, root, status.getPath()); } LOG.info(String.format("Wrote %d entries for directory %s", count, itemToZip)); return count; } final String inZipPath = relativePathForZipEntry(itemToZip.toUri().getPath(), baseName, root); if (inZipPath.length() == 0) { LOG.warn(String.format("Skipping empty zip file path for %s (%s %s)", itemToZip, root, baseName)); return 0; } // Take empty files in case the place holder is needed FSDataInputStream in = null; try { in = localFs.open(itemToZip); final ZipEntry ze = new ZipEntry(inZipPath); ze.setTime(itemStatus.getModificationTime()); // Comments confuse looking at the zip file // ze.setComment(itemToZip.toString()); zos.putNextEntry(ze); IOUtils.copyBytes(in, zos, conf, false); zos.closeEntry(); LOG.info(String.format("Wrote %d entries for file %s", count, itemToZip)); return 1; } finally { in.close(); } }
From source file:org.agnitas.util.ZipUtilities.java
/** * Compress a file or recursively compress all files of a folder. * /*www . j av a 2 s . c o m*/ * @param sourceFile * @param destinationZipFileSream * @throws IOException */ public static void addFileToOpenZipFileStream(File sourceFile, String relativeDirPath, ZipOutputStream destinationZipFileSream) throws IOException { BufferedInputStream bufferedFileInputStream = null; if (!sourceFile.exists()) throw new IOException("SourceFile does not exist"); if (destinationZipFileSream == null) throw new IOException("DestinationStream is not ready"); if (relativeDirPath == null || (!relativeDirPath.endsWith("/") && !relativeDirPath.endsWith("\\"))) throw new IOException("RelativeDirPath is invalid"); try { if (!sourceFile.isDirectory()) { ZipEntry entry = new ZipEntry(relativeDirPath + sourceFile.getName()); entry.setTime(sourceFile.lastModified()); destinationZipFileSream.putNextEntry(entry); bufferedFileInputStream = new BufferedInputStream(new FileInputStream(sourceFile)); byte[] bufferArray = new byte[1024]; int byteBufferFillLength = bufferedFileInputStream.read(bufferArray); while (byteBufferFillLength > -1) { destinationZipFileSream.write(bufferArray, 0, byteBufferFillLength); byteBufferFillLength = bufferedFileInputStream.read(bufferArray); } bufferedFileInputStream.close(); bufferedFileInputStream = null; destinationZipFileSream.flush(); destinationZipFileSream.closeEntry(); } else { for (File sourceSubFile : sourceFile.listFiles()) { addFileToOpenZipFileStream(sourceSubFile, relativeDirPath + sourceFile.getName() + File.separator, destinationZipFileSream); } } } catch (IOException e) { throw e; } finally { if (bufferedFileInputStream != null) { try { bufferedFileInputStream.close(); } catch (Exception e) { } bufferedFileInputStream = null; } } }
From source file:lucee.commons.io.compress.CompressUtil.java
private static void compressZip(String parent, Resource source, ZipOutputStream zos, ResourceFilter filter) throws IOException { if (source.isFile()) { //if(filter.accept(source)) { ZipEntry ze = new ZipEntry(parent); ze.setTime(source.lastModified()); zos.putNextEntry(ze);/* ww w. ja v a 2s . c om*/ try { IOUtil.copy(source, zos, false); } finally { zos.closeEntry(); } //} } else if (source.isDirectory()) { if (!StringUtil.isEmpty(parent)) { ZipEntry ze = new ZipEntry(parent + "/"); ze.setTime(source.lastModified()); try { zos.putNextEntry(ze); } catch (IOException ioe) { if (Caster.toString(ioe.getMessage()).indexOf("duplicate") == -1) throw ioe; } zos.closeEntry(); } compressZip(parent, filter == null ? source.listResources() : source.listResources(filter), zos, filter); } }
From source file:it.cnr.icar.eric.common.Utility.java
public static ZipOutputStream createZipOutputStream(String baseDir, String[] relativeFilePaths, OutputStream os) throws FileNotFoundException, IOException { if (baseDir.startsWith("file:/")) { baseDir = baseDir.substring(5);/*ww w . j a v a 2s . c o m*/ } ZipOutputStream zipoutputstream = new ZipOutputStream(os); zipoutputstream.setMethod(ZipOutputStream.STORED); for (int i = 0; i < relativeFilePaths.length; i++) { File file = new File(baseDir + FILE_SEPARATOR + relativeFilePaths[i]); byte[] buffer = new byte[1000]; int n; FileInputStream fis; // Calculate the CRC-32 value. This isn't strictly necessary // for deflated entries, but it doesn't hurt. CRC32 crc32 = new CRC32(); fis = new FileInputStream(file); while ((n = fis.read(buffer)) > -1) { crc32.update(buffer, 0, n); } fis.close(); // Create a zip entry. ZipEntry zipEntry = new ZipEntry(relativeFilePaths[i]); zipEntry.setSize(file.length()); zipEntry.setTime(file.lastModified()); zipEntry.setCrc(crc32.getValue()); // Add the zip entry and associated data. zipoutputstream.putNextEntry(zipEntry); fis = new FileInputStream(file); while ((n = fis.read(buffer)) > -1) { zipoutputstream.write(buffer, 0, n); } fis.close(); zipoutputstream.closeEntry(); } return zipoutputstream; }
From source file:org.eclipse.cft.server.core.internal.CloudUtil.java
private static void addZipEntries(ZipOutputStream out, List<IModuleResource> allResources, Set<IModuleResource> filterInFiles) throws Exception { if (allResources == null) return;/*from w w w.j a v a 2 s. c o m*/ for (IModuleResource resource : allResources) { if (resource instanceof IModuleFolder) { IModuleResource[] folderResources = ((IModuleFolder) resource).members(); String entryPath = getZipRelativeName(resource); ZipEntry zipEntry = new ZipEntry(entryPath); long timeStamp = 0; IContainer folder = (IContainer) resource.getAdapter(IContainer.class); if (folder != null) { timeStamp = folder.getLocalTimeStamp(); } if (timeStamp != IResource.NULL_STAMP && timeStamp != 0) { zipEntry.setTime(timeStamp); } out.putNextEntry(zipEntry); out.closeEntry(); addZipEntries(out, Arrays.asList(folderResources), filterInFiles); continue; } IModuleFile moduleFile = (IModuleFile) resource; // Only add files that are in the filterInList if (!filterInFiles.contains(moduleFile)) { continue; } String entryPath = getZipRelativeName(resource); ZipEntry zipEntry = new ZipEntry(entryPath); InputStream input = null; long timeStamp = 0; IFile iFile = (IFile) moduleFile.getAdapter(IFile.class); if (iFile != null) { timeStamp = iFile.getLocalTimeStamp(); input = iFile.getContents(); } else { File file = (File) moduleFile.getAdapter(File.class); timeStamp = file.lastModified(); input = new FileInputStream(file); } if (timeStamp != IResource.NULL_STAMP && timeStamp != 0) { zipEntry.setTime(timeStamp); } out.putNextEntry(zipEntry); try { int n = 0; while (n > -1) { n = input.read(buf); if (n > 0) { out.write(buf, 0, n); } } } finally { input.close(); } out.closeEntry(); } }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
/** * ?zip?solib?//w w w . j a v a2s .c o m * * @param output * @param srcDir * @throws Exception */ public static void addFileAndDirectoryToZip(File output, File srcDir) throws Exception { if (output.isDirectory()) { throw new IOException("This is a directory!"); } if (!output.getParentFile().exists()) { output.getParentFile().mkdirs(); } if (!output.exists()) { output.createNewFile(); } List fileList = getSubFiles(srcDir); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output)); ZipEntry ze = null; byte[] buf = new byte[1024]; int readLen = 0; for (int i = 0; i < fileList.size(); i++) { File f = (File) fileList.get(i); ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f)); ze.setSize(f.length()); ze.setTime(f.lastModified()); zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(f)); while ((readLen = is.read(buf, 0, 1024)) != -1) { zos.write(buf, 0, readLen); } is.close(); } zos.close(); }
From source file:org.opencms.workplace.tools.git.CmsGitCheckin.java
/** * Creates ZIP file data from the files / subfolders of the given root folder, and sends it to the given stream.<p> * * The stream passed as an argument is closed after the data is written. * * @param root the folder to zip//from ww w. j a v a2 s . c o m * @param zipOutput the output stream which the zip file data should be written to * * @throws Exception if something goes wrong */ public static void zipRfsFolder(final File root, final OutputStream zipOutput) throws Exception { final ZipOutputStream zip = new ZipOutputStream(zipOutput); try { CmsFileUtil.walkFileSystem(root, new Closure() { @SuppressWarnings("resource") public void execute(Object stateObj) { try { FileWalkState state = (FileWalkState) stateObj; for (File file : state.getFiles()) { String relativePath = Paths.get(root.getAbsolutePath()) .relativize(Paths.get(file.getAbsolutePath())).toString(); ZipEntry entry = new ZipEntry(relativePath); entry.setTime(file.lastModified()); zip.putNextEntry(entry); zip.write(CmsFileUtil.readFully(new FileInputStream(file))); zip.closeEntry(); } } catch (Exception e) { throw new RuntimeException(e); } } }); } catch (RuntimeException e) { if (e.getCause() instanceof Exception) { throw (Exception) (e.getCause()); } else { throw e; } } zip.flush(); zip.close(); }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
public static void addFileAndDirectoryToZip(File output, File srcDir, Map<String, ZipEntry> zipEntryMethodMap) throws Exception { if (output.isDirectory()) { throw new IOException("This is a directory!"); }// w w w .j av a 2 s . c om if (!output.getParentFile().exists()) { output.getParentFile().mkdirs(); } if (!output.exists()) { output.createNewFile(); } List fileList = getSubFiles(srcDir); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output)); ZipEntry ze = null; byte[] buf = new byte[1024]; int readLen = 0; for (int i = 0; i < fileList.size(); i++) { File f = (File) fileList.get(i); ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f)); ze.setSize(f.length()); ze.setTime(f.lastModified()); if (zipEntryMethodMap != null) { ZipEntry originEntry = zipEntryMethodMap.get(f.getAbsolutePath()); if (originEntry != null) { ze.setCompressedSize(originEntry.getCompressedSize()); ze.setCrc(originEntry.getCrc()); ze.setMethod(originEntry.getMethod()); } } zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(f)); while ((readLen = is.read(buf, 0, 1024)) != -1) { zos.write(buf, 0, readLen); } is.close(); } zos.close(); }
From source file:com.taobao.android.builder.tools.zip.ZipUtils.java
public static void rezip(File output, File srcDir, Map<String, ZipEntry> zipEntryMethodMap) throws Exception { if (output.isDirectory()) { throw new IOException("This is a directory!"); }//from w w w.ja v a2s .co m if (!output.getParentFile().exists()) { output.getParentFile().mkdirs(); } if (!output.exists()) { output.createNewFile(); } List fileList = getSubFiles(srcDir); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output)); ZipEntry ze = null; byte[] buf = new byte[1024]; int readLen = 0; for (int i = 0; i < fileList.size(); i++) { File f = (File) fileList.get(i); ze = new ZipEntry(getAbsFileName(srcDir.getPath(), f)); ze.setSize(f.length()); ze.setTime(f.lastModified()); if (zipEntryMethodMap != null) { ZipEntry originEntry = zipEntryMethodMap.get(ze.getName()); if (originEntry != null) { if (originEntry.getMethod() == STORED) { ze.setCompressedSize(f.length()); InputStream in = new BufferedInputStream(new FileInputStream(f)); try { CRC32 crc = new CRC32(); int c; while ((c = in.read()) != -1) { crc.update(c); } ze.setCrc(crc.getValue()); } finally { in.close(); } } ze.setMethod(originEntry.getMethod()); } } zos.putNextEntry(ze); InputStream is = new BufferedInputStream(new FileInputStream(f)); while ((readLen = is.read(buf, 0, 1024)) != -1) { zos.write(buf, 0, readLen); } is.close(); } zos.close(); }