List of usage examples for java.util.zip ZipOutputStream finish
public void finish() throws IOException
From source file:org.geowebcache.service.kml.KMZHelper.java
/** * Writes two byte[] into a zip/*from w ww . j a va2s . c om*/ * -> like a zipfile with two files * * @param namePfx prefix for files inside file * @param overlay * @param data * @param out * @throws IOException */ private static void writeZippedKML(String namePfx, String formatExtension, byte[] overlay, Resource data, OutputStream out) throws IOException { ZipOutputStream zipos = new ZipOutputStream(out); // High compression //zipos.setLevel(9); // Add the overlay, links to the next content ZipEntry zeOl = new ZipEntry("netlinks_" + namePfx + ".kml"); zipos.putNextEntry(zeOl); zipos.write(overlay); // Add the actual data, if applicable if (data != null) { ZipEntry zeData = new ZipEntry("data_" + namePfx + "." + formatExtension); zipos.putNextEntry(zeData); WritableByteChannel outch = Channels.newChannel(zipos); data.transferTo(outch); } zipos.finish(); }
From source file:net.grinder.util.LogCompressUtil.java
/** * Compress the given file.// w w w. jav a2 s. c om * * @param logFile * file to be compressed * @return compressed file byte array */ public static byte[] compressFile(File logFile) { FileInputStream fio = null; ByteArrayOutputStream out = null; ZipOutputStream zos = null; try { fio = new FileInputStream(logFile); out = new ByteArrayOutputStream(); zos = new ZipOutputStream(out); ZipEntry zipEntry = new ZipEntry("log.txt"); zipEntry.setTime(new Date().getTime()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[COMPRESS_BUFFER_SIZE]; int count = 0; while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) { zos.write(buffer, 0, count); } zos.closeEntry(); zos.finish(); zos.flush(); return out.toByteArray(); } catch (IOException e) { LOGGER.error("Error occurs while compress {}", logFile.getAbsolutePath()); LOGGER.error("Details", e); return null; } finally { IOUtils.closeQuietly(zos); IOUtils.closeQuietly(fio); IOUtils.closeQuietly(out); } }
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 2s . co 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:Main.java
private static void zipDir(String dir, ZipOutputStream out) throws IOException { File directory = new File(dir); URI base = directory.toURI(); ArrayList<File> filesToZip = new ArrayList<File>(); GetFiles(directory, filesToZip);//from w w w . j a v a 2 s .co m for (int i = 0; i < filesToZip.size(); ++i) { FileInputStream in = new FileInputStream(filesToZip.get(i)); String name = base.relativize(filesToZip.get(i).toURI()).getPath(); out.putNextEntry(new ZipEntry(name)); byte[] buf = new byte[4096]; int bytes = 0; while ((bytes = in.read(buf)) != -1) { out.write(buf, 0, bytes); } out.closeEntry(); in.close(); } out.finish(); out.flush(); }
From source file:org.apache.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/* w w w .jav a2s .com*/ */ 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:com.recomdata.transmart.data.export.util.ZipUtil.java
/** * This method will bundle all the files into a zip file. * If there are 2 files with the same name, only the first file is part of the zip. * /*w w w . ja va2 s . co m*/ * @param zipFileName * @param files * * @return zipFile absolute path * */ public static String bundleZipFile(String zipFileName, List<File> files) { File zipFile = null; Map<String, File> filesMap = new HashMap<String, File>(); if (StringUtils.isEmpty(zipFileName)) return null; try { zipFile = new File(zipFileName); if (zipFile.exists() && zipFile.isFile() && zipFile.delete()) { zipFile = new File(zipFileName); } ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); zipOut.setLevel(ZipOutputStream.DEFLATED); byte[] buffer = new byte[BUFFER_SIZE]; for (File file : files) { if (filesMap.containsKey(file.getName())) { continue; } else if (file.exists() && file.canRead()) { filesMap.put(file.getName(), file); zipOut.putNextEntry(new ZipEntry(file.getName())); FileInputStream fis = new FileInputStream(file); int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { zipOut.write(buffer, 0, bytesRead); } zipOut.flush(); zipOut.closeEntry(); } } zipOut.finish(); zipOut.close(); } catch (IOException e) { //log.error("Error while creating Zip file"); } return (null != zipFile) ? zipFile.getAbsolutePath() : null; }
From source file:pxb.android.dex2jar.dump.Dump.java
public static void doData(byte[] data, File destJar) throws IOException { final ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destJar))); new DexFileReader(data).accept(new Dump(new EmptyVisitor(), new WriterManager() { public PrintWriter get(String name) { try { String s = name.replace('.', '/') + ".dump.txt"; ZipEntry zipEntry = new ZipEntry(s); zos.putNextEntry(zipEntry); return new PrintWriter(new ProxyOutputStream(zos) { @Override/*from www .j a va 2 s. com*/ public void close() throws IOException { zos.closeEntry(); } }); } catch (Exception e) { throw new RuntimeException(e); } } })); zos.finish(); zos.close(); }
From source file:org.geoserver.csw.response.ZipOutputFormat.java
@Override public void write(Object value, OutputStream output, Operation operation) throws IOException, ServiceException { File tempDir = IOUtils.createTempDirectory("ziptemp"); if (value == null) { throw new ServiceException("No value to be written has been provided"); }/* w w w.j a va 2 s. c o m*/ try { List<File> files = null; if (value instanceof List) { files = (List<File>) value; } else if (value instanceof File) { files = Collections.singletonList((File) value); } else { throw new IllegalArgumentException(value.getClass() + " type isn't supported yet"); } // Copying files to the temp folder for (File file : files) { FileUtils.copyFile(file, new File(tempDir, file.getName())); } ZipOutputStream zipOut = new ZipOutputStream(output); IOUtils.zipDirectory(tempDir, zipOut, null); zipOut.finish(); } finally { // make sure we remove the temp directory and its contents completely now try { FileUtils.deleteDirectory(tempDir); } catch (IOException e) { LOGGER.warning("Could not delete temp directory: " + tempDir.getAbsolutePath() + " due to: " + e.getMessage()); } } }
From source file:com.google.mr4c.sources.LogsDatasetSource.java
public synchronized void copyToFinal() throws IOException { List<String> names = new ArrayList<String>(m_fileSrc.getAllFileNames()); names.remove(ZIP_FILE_NAME);/*from w ww . ja v a 2 s.c om*/ DataFileSink zipSink = m_fileSrc.getFileSink(ZIP_FILE_NAME); OutputStream output = zipSink.getFileOutputStream(); try { ZipOutputStream zipStream = new ZipOutputStream(output); for (String name : names) { addZipEntry(zipStream, name); } zipStream.finish(); } finally { output.close(); } }
From source file:net.grinder.util.LogCompressUtil.java
/** * Compress multiple Files.//from w w w . j a v a2 s . co m * * @param logFiles * files to be compressed * @return compressed file byte array */ public static byte[] compressFile(File[] logFiles) { FileInputStream fio = null; ByteArrayOutputStream out = null; ZipOutputStream zos = null; try { out = new ByteArrayOutputStream(); zos = new ZipOutputStream(out); for (File each : logFiles) { try { fio = new FileInputStream(each); ZipEntry zipEntry = new ZipEntry(each.getName()); zipEntry.setTime(each.lastModified()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[COMPRESS_BUFFER_SIZE]; int count = 0; while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) { zos.write(buffer, 0, count); } zos.closeEntry(); } catch (IOException e) { LOGGER.error("Error occurs while compress {}", each.getAbsolutePath()); LOGGER.error("Details", e); } finally { IOUtils.closeQuietly(fio); } } zos.finish(); zos.flush(); return out.toByteArray(); } catch (IOException e) { LOGGER.info("Error occurs while compress log : {} ", e.getMessage()); LOGGER.debug("Details", e); return null; } finally { IOUtils.closeQuietly(zos); IOUtils.closeQuietly(fio); IOUtils.closeQuietly(out); } }