List of usage examples for java.util.zip ZipOutputStream putNextEntry
public void putNextEntry(ZipEntry e) throws IOException
From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java
public static void addFolderToZip(File folder, String parentFolder, ZipOutputStream zos) throws Exception { for (File file : folder.listFiles()) { if (file.isDirectory()) { zos.putNextEntry(new ZipEntry(parentFolder + file.getName() + "/")); zos.closeEntry();/* w w w .jav a2s .c o m*/ addFolderToZip(file, parentFolder + file.getName() + "/", zos); continue; } else { zos.putNextEntry(new ZipEntry(parentFolder + file.getName())); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); long bytesRead = 0; byte[] bytesIn = new byte[1024]; int read = 0; while ((read = bis.read(bytesIn)) != -1) { zos.write(bytesIn, 0, read); bytesRead += read; } zos.closeEntry(); bis.close(); } } }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
public static void zip(OutputStream outputStream, Map<String, InputStream> filePaths) throws IOException { ZipOutputStream out = new ZipOutputStream(outputStream); for (Map.Entry<String, InputStream> file : filePaths.entrySet()) { String filename = file.getKey(); InputStream stream = file.getValue(); if (stream != null) { System.out.println("Adding: " + filename); ZipEntry entry = new ZipEntry(filename); out.putNextEntry(entry); IOUtils.copy(stream, out);/*from w w w. ja va2s . co m*/ stream.close(); } } out.close(); }
From source file:com.asakusafw.testdriver.compiler.util.DeploymentUtil.java
private static void putEntry(ZipOutputStream zip, File source, String path, Set<String> saw) throws IOException { assert zip != null; assert source != null; assert !(source.isFile() && path == null); if (source.isDirectory()) { for (File child : list(source)) { String next = (path == null) ? child.getName() : path + '/' + child.getName(); putEntry(zip, child, next, saw); }/*from w w w .ja v a2 s. co m*/ } else { if (saw.contains(path)) { return; } saw.add(path); zip.putNextEntry(new ZipEntry(path)); try (InputStream in = new BufferedInputStream(new FileInputStream(source))) { LOG.trace("Copy into archive: {} -> {}", source, path); copyStream(in, zip); } zip.closeEntry(); } }
From source file:Main.java
/** * Zips a subfolder//from w ww. j av a 2 s . c om */ protected static void zipSubFolder(ZipOutputStream zipOut, File srcFolder, int basePathLength) throws IOException { final int BUFFER = 2048; File[] fileList = srcFolder.listFiles(); BufferedInputStream bis = null; for (File file : fileList) { if (file.isDirectory()) { zipSubFolder(zipOut, file, basePathLength); } else { byte data[] = new byte[BUFFER]; String unmodifiedFilePath = file.getPath(); String relativePath = unmodifiedFilePath.substring(basePathLength); Log.d("ZIP SUBFOLDER", "Relative Path : " + relativePath); FileInputStream fis = new FileInputStream(unmodifiedFilePath); bis = new BufferedInputStream(fis, BUFFER); ZipEntry zipEntry = new ZipEntry(relativePath); zipOut.putNextEntry(zipEntry); int count; while ((count = bis.read(data, 0, BUFFER)) != -1) { zipOut.write(data, 0, count); } bis.close(); } } }
From source file:com.mvdb.etl.actions.ActionUtils.java
private static void zipDir(String origDir, File dirObj, ZipOutputStream zos) throws IOException { File[] files = dirObj.listFiles(); byte[] tmpBuf = new byte[1024]; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { zipDir(origDir, files[i], zos); continue; }/* w ww . j av a 2s . com*/ String wAbsolutePath = files[i].getAbsolutePath().substring(origDir.length() + 1, files[i].getAbsolutePath().length()); FileInputStream in = new FileInputStream(files[i].getAbsolutePath()); zos.putNextEntry(new ZipEntry(wAbsolutePath)); int len; while ((len = in.read(tmpBuf)) > 0) { zos.write(tmpBuf, 0, len); } zos.closeEntry(); in.close(); } }
From source file:au.org.ands.vocabs.toolkit.utils.ToolkitFileUtils.java
/** Compress the files in the backup folder for a project. * @param projectId The project ID//from www .j a v a2s . c o m * @throws IOException Any exception when reading/writing data. */ public static void compressBackupFolder(final String projectId) throws IOException { String backupPath = getBackupPath(projectId); if (!Files.isDirectory(Paths.get(backupPath))) { // No such directory, so nothing to do. return; } String projectSlug = makeSlug(projectId); // The name of the ZIP file that does/will contain all // backups for this project. Path zipFilePath = Paths.get(backupPath).resolve(projectSlug + ".zip"); // A temporary ZIP file. Any existing content in the zipFilePath // will be copied into this, followed by any other files in // the directory that have not yet been added. Path tempZipFilePath = Paths.get(backupPath).resolve("temp" + ".zip"); File tempZipFile = tempZipFilePath.toFile(); if (!tempZipFile.exists()) { tempZipFile.createNewFile(); } ZipOutputStream tempZipOut = new ZipOutputStream(new FileOutputStream(tempZipFile)); File existingZipFile = zipFilePath.toFile(); if (existingZipFile.exists()) { ZipFile zipIn = new ZipFile(existingZipFile); Enumeration<? extends ZipEntry> entries = zipIn.entries(); while (entries.hasMoreElements()) { ZipEntry e = entries.nextElement(); logger.debug("compressBackupFolder copying: " + e.getName()); tempZipOut.putNextEntry(e); if (!e.isDirectory()) { copy(zipIn.getInputStream(e), tempZipOut); } tempZipOut.closeEntry(); } zipIn.close(); } File dir = new File(backupPath); File[] files = dir.listFiles(); for (File source : files) { if (!source.getName().toLowerCase().endsWith(".zip")) { logger.debug("compressBackupFolder compressing and " + "deleting file: " + source.toString()); if (zipFile(tempZipOut, source)) { source.delete(); } } } tempZipOut.flush(); tempZipOut.close(); tempZipFile.renameTo(existingZipFile); }
From source file:io.druid.java.util.common.CompressionUtils.java
/** * Zips the contents of the input directory to the output stream. Sub directories are skipped * * @param directory The directory whose contents should be added to the zip in the output stream. * @param out The output stream to write the zip data to. Caller is responsible for closing this stream. * * @return The number of bytes (uncompressed) read from the input directory. * * @throws IOException//from w w w . j a va 2 s .c o m */ public static long zip(File directory, OutputStream out) throws IOException { if (!directory.isDirectory()) { throw new IOE("directory[%s] is not a directory", directory); } final ZipOutputStream zipOut = new ZipOutputStream(out); long totalSize = 0; for (File file : directory.listFiles()) { log.info("Adding file[%s] with size[%,d]. Total size so far[%,d]", file, file.length(), totalSize); if (file.length() >= Integer.MAX_VALUE) { zipOut.finish(); throw new IOE("file[%s] too large [%,d]", file, file.length()); } zipOut.putNextEntry(new ZipEntry(file.getName())); totalSize += Files.asByteSource(file).copyTo(zipOut); } zipOut.closeEntry(); // Workaround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf zipOut.flush(); zipOut.finish(); return totalSize; }
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 w w. j a v a 2s .c o 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.asakusafw.testdriver.compiler.util.DeploymentUtil.java
private static void mergeEntries(ZipOutputStream zip, File file, Set<String> saw) throws IOException { try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)))) { while (true) { ZipEntry entry = in.getNextEntry(); if (entry == null) { break; }/*from ww w.ja v a 2s . com*/ if (saw.contains(entry.getName())) { continue; } if (LOG.isTraceEnabled()) { LOG.trace("Copy into archive: {} -> {}", entry.getName(), file); } saw.add(entry.getName()); zip.putNextEntry(new ZipEntry(entry.getName())); copyStream(in, zip); } } }
From source file:net.minecraftforge.fml.common.asm.transformers.MarkerTransformer.java
private static void processJar(File inFile, File outFile, MarkerTransformer[] transformers) throws IOException { ZipInputStream inJar = null;/* w w w . j a va 2 s . c o m*/ ZipOutputStream outJar = null; try { try { inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile))); } catch (FileNotFoundException e) { throw new FileNotFoundException("Could not open input file: " + e.getMessage()); } try { outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile))); } catch (FileNotFoundException e) { throw new FileNotFoundException("Could not open output file: " + e.getMessage()); } ZipEntry entry; while ((entry = inJar.getNextEntry()) != null) { if (entry.isDirectory()) { outJar.putNextEntry(entry); continue; } byte[] data = new byte[4096]; ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream(); int len; do { len = inJar.read(data); if (len > 0) { entryBuffer.write(data, 0, len); } } while (len != -1); byte[] entryData = entryBuffer.toByteArray(); String entryName = entry.getName(); if (entryName.endsWith(".class") && !entryName.startsWith(".")) { ClassNode cls = new ClassNode(); ClassReader rdr = new ClassReader(entryData); rdr.accept(cls, 0); String name = cls.name.replace('/', '.').replace('\\', '.'); for (MarkerTransformer trans : transformers) { entryData = trans.transform(name, name, entryData); } } ZipEntry newEntry = new ZipEntry(entryName); outJar.putNextEntry(newEntry); outJar.write(entryData); } } finally { IOUtils.closeQuietly(outJar); IOUtils.closeQuietly(inJar); } }