List of usage examples for java.util.zip ZipEntry ZipEntry
public ZipEntry(ZipEntry e)
From source file:com.smash.revolance.ui.model.helper.ArchiveHelper.java
public static File buildArchive(File archive, File... files) throws FileNotFoundException { FileOutputStream fos = new FileOutputStream(archive); ZipOutputStream zos = new ZipOutputStream(fos); int bytesRead; byte[] buffer = new byte[1024]; CRC32 crc = new CRC32(); for (File file : files) { if (!file.exists()) { System.err.println("Skipping: " + file); continue; }/*from www. j a va 2 s . c o m*/ BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(file)); crc.reset(); while ((bytesRead = bis.read(buffer)) != -1) { crc.update(buffer, 0, bytesRead); } bis.close(); // Reset to beginning of input stream bis = new BufferedInputStream(new FileInputStream(file)); String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file); ZipEntry entry = new ZipEntry(entryPath); entry.setMethod(ZipEntry.STORED); entry.setCompressedSize(file.length()); entry.setSize(file.length()); entry.setCrc(crc.getValue()); zos.putNextEntry(entry); while ((bytesRead = bis.read(buffer)) != -1) { zos.write(buffer, 0, bytesRead); } } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } finally { IOUtils.closeQuietly(bis); } } IOUtils.closeQuietly(zos); return archive; }
From source file:com.facebook.buck.util.zip.ZipScrubberTest.java
@Test public void modificationTimes() throws Exception { // Create a dummy ZIP file. ByteArrayOutputStream bytesOutputStream = new ByteArrayOutputStream(); try (ZipOutputStream out = new ZipOutputStream(bytesOutputStream)) { ZipEntry entry = new ZipEntry("file1"); byte[] data = "data1".getBytes(Charsets.UTF_8); entry.setSize(data.length);//from ww w . j a va 2 s .c o m out.putNextEntry(entry); out.write(data); out.closeEntry(); entry = new ZipEntry("file2"); data = "data2".getBytes(Charsets.UTF_8); entry.setSize(data.length); out.putNextEntry(entry); out.write(data); out.closeEntry(); } byte[] bytes = bytesOutputStream.toByteArray(); // Execute the zip scrubber step. 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)); } } }
From source file:FileUtil.java
/** * Zip up a directory path/* w w w .ja v a 2 s . c om*/ * @param directory * @param zos * @param path * @throws IOException */ public static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException { File zipDir = new File(directory); // get a listing of the directory content String[] dirList = zipDir.list(); byte[] readBuffer = new byte[2156]; int bytesIn = 0; // loop through dirList, and zip the files for (int i = 0; i < dirList.length; i++) { File f = new File(zipDir, dirList[i]); if (f.isDirectory()) { String filePath = f.getPath(); zipDir(filePath, zos, path + f.getName() + "/"); continue; } FileInputStream fis = new FileInputStream(f); try { ZipEntry anEntry = new ZipEntry(path + f.getName()); zos.putNextEntry(anEntry); bytesIn = fis.read(readBuffer); while (bytesIn != -1) { zos.write(readBuffer, 0, bytesIn); bytesIn = fis.read(readBuffer); } } finally { fis.close(); } } }
From source file:Main.java
private static void addFileToZip(String sourceFolderPath, String sourceFilePath, String baseFolderPath, ZipOutputStream zos) throws Exception { File item = new File(sourceFilePath); if (item == null || !item.exists()) return; //skip if the file is not exist if (isSymLink(item)) return; // do nothing to symbolic links. if (baseFolderPath == null) baseFolderPath = ""; if (item.isDirectory()) { for (String subItem : item.list()) { addFileToZip(sourceFolderPath + File.separator + item.getName(), sourceFilePath + File.separator + subItem, baseFolderPath, zos); }//w ww. j a v a 2s.co m } else { byte[] buf = new byte[102400]; //100k buffer int len; FileInputStream inStream = new FileInputStream(sourceFilePath); if (baseFolderPath.equals("")) //sourceFiles in absolute path, zip the file with absolute path zos.putNextEntry(new ZipEntry(sourceFilePath)); else {//relative path String relativePath = sourceFilePath.substring(baseFolderPath.length()); zos.putNextEntry(new ZipEntry(relativePath)); } while ((len = inStream.read(buf)) > 0) { zos.write(buf, 0, len); } } }
From source file:net.grinder.util.LogCompressUtils.java
/** * Compress multiple Files with the given encoding. * * @param logFiles files to be compressed * @param fromEncoding log file encoding * @param toEncoding compressed log file encoding * @return compressed file byte array/* w ww. j a v a2 s. co m*/ */ public static byte[] compress(File[] logFiles, Charset fromEncoding, Charset toEncoding) { FileInputStream fis = null; InputStreamReader isr = null; ByteArrayOutputStream out = null; ZipOutputStream zos = null; OutputStreamWriter osw = null; if (toEncoding == null) { toEncoding = Charset.defaultCharset(); } if (fromEncoding == null) { fromEncoding = Charset.defaultCharset(); } try { out = new ByteArrayOutputStream(); zos = new ZipOutputStream(out); osw = new OutputStreamWriter(zos, toEncoding); for (File each : logFiles) { try { fis = new FileInputStream(each); isr = new InputStreamReader(fis, fromEncoding); ZipEntry zipEntry = new ZipEntry(each.getName()); zipEntry.setTime(each.lastModified()); zos.putNextEntry(zipEntry); char[] buffer = new char[COMPRESS_BUFFER_SIZE]; int count; while ((count = isr.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) { osw.write(buffer, 0, count); } osw.flush(); zos.flush(); zos.closeEntry(); } catch (IOException e) { LOGGER.error("Error occurs while compressing {} : {}", each.getAbsolutePath(), e.getMessage()); LOGGER.debug("Details ", e); } finally { IOUtils.closeQuietly(isr); IOUtils.closeQuietly(fis); } } zos.finish(); zos.flush(); return out.toByteArray(); } catch (IOException e) { LOGGER.error("Error occurs while compressing log : {} ", e.getMessage()); LOGGER.debug("Details : ", e); return null; } finally { IOUtils.closeQuietly(zos); IOUtils.closeQuietly(out); IOUtils.closeQuietly(osw); } }
From source file:com.youTransactor.uCube.LogManager.java
public static void getLogs(OutputStream out) throws IOException { File logDir = context.getDir(LOG_DIR, Context.MODE_PRIVATE); if (logDir.listFiles().length > 0) { ZipOutputStream zout = new ZipOutputStream(out); for (File file : logDir.listFiles()) { ZipEntry entry = new ZipEntry(file.getName()); zout.putNextEntry(entry);/*from w ww .j a v a2s. com*/ IOUtils.copy(new FileInputStream(file), zout); zout.closeEntry(); } zout.close(); } }
From source file:com.perrier.music.entity.album.AlbumZipper.java
public File zip(Album album) throws IOException { Path zipPath = Files.createTempFile(ZIP_PREFIX + album.getId() + "-", ".zip"); File zipFile = zipPath.toFile(); try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) { List<Track> tracks = album.getTracks(); for (Track t : tracks) { String name = createFileName(t); ZipEntry zipEntry = new ZipEntry(name); zos.putNextEntry(zipEntry);/* w ww . j av a2 s .c o m*/ try (InputStream audioStream = this.storageService.getAudioStream(t.getAudioStorageKey())) { IOUtils.copy(audioStream, zos); } } } return zipFile; }
From source file:cc.recommenders.utils.Zips.java
public static void zip(File directory, File out) throws IOException { ZipOutputStream zos = null;//from ww w .ja v a 2s.c o m try { OutputSupplier<FileOutputStream> s = Files.newOutputStreamSupplier(out); zos = new ZipOutputStream(s.getOutput()); Collection<File> files = FileUtils.listFiles(directory, FILE, DIRECTORY); for (File f : files) { String path = removeStart(f.getPath(), directory.getAbsolutePath() + "/"); ZipEntry e = new ZipEntry(path); zos.putNextEntry(e); byte[] data = Files.toByteArray(f); zos.write(data); zos.closeEntry(); } } finally { Closeables.close(zos, false); } }
From source file:com.yqboots.fss.util.ZipUtils.java
/** * Compresses the specified directory to a zip file * * @param dir the directory to compress/*from w w w .j a va 2s .com*/ * @return the compressed file * @throws IOException */ public static Path compress(Path dir) throws IOException { Assert.isTrue(Files.exists(dir), "The directory does not exist: " + dir.toAbsolutePath()); Assert.isTrue(Files.isDirectory(dir), "Should be a directory: " + dir.toAbsolutePath()); Path result = Paths.get(dir.toAbsolutePath() + FileType.DOT_ZIP); try (final ZipOutputStream out = new ZipOutputStream( new BufferedOutputStream(new FileOutputStream(result.toFile())))) { // out.setMethod(ZipOutputStream.DEFLATED); final byte data[] = new byte[BUFFER]; // get a list of files from current directory Files.walkFileTree(dir, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs) throws IOException { final File file = path.toFile(); // compress to relative directory, not absolute final String root = StringUtils.substringAfter(file.getParent(), dir.toString()); try (final BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file), BUFFER)) { final ZipEntry entry = new ZipEntry(root + File.separator + path.getFileName()); out.putNextEntry(entry); int count; while ((count = origin.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } } return FileVisitResult.CONTINUE; } }); } return result; }
From source file:Compress.java
/** Zip the contents of the directory, and save it in the zipfile */ public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException { // Check that the directory is a directory, and get its contents File d = new File(dir); if (!d.isDirectory()) throw new IllegalArgumentException("Compress: not a directory: " + dir); String[] entries = d.list();/* w ww.j av a2 s .c o m*/ byte[] buffer = new byte[4096]; // Create a buffer for copying int bytes_read; // Create a stream to compress data and write it to the zipfile ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); // Loop through all entries in the directory for (int i = 0; i < entries.length; i++) { File f = new File(d, entries[i]); if (f.isDirectory()) continue; // Don't zip sub-directories FileInputStream in = new FileInputStream(f); // Stream to read file ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry out.putNextEntry(entry); // Store entry while ((bytes_read = in.read(buffer)) != -1) // Copy bytes out.write(buffer, 0, bytes_read); in.close(); // Close input stream } // When we're done with the whole loop, close the output stream out.close(); }