List of usage examples for java.util.zip ZipOutputStream flush
public void flush() throws IOException
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 ww w. j av a 2s . c om */ 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: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/*from w ww .j a v a 2s.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:com.mosso.client.cloudfiles.sample.FilesCopy.java
/** * * @param folder//from w w w . j a va 2 s .co m * @return null if the input is not a folder otherwise a zip file containing all the files in the folder with nested folders skipped. * @throws IOException */ public static File zipFolder(File folder) throws IOException { byte[] buf = new byte[1024]; int len; // Create the ZIP file String filenameWithZipExt = folder.getName() + ZIPEXTENSION; File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile)); if (folder.isDirectory()) { File[] files = folder.listFiles(); for (File f : files) { if (!f.isDirectory()) { FileInputStream in = new FileInputStream(f); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(f.getName())); // Transfer bytes from the file to the ZIP file while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } else logger.warn("Skipping nested folder: " + f.getAbsoluteFile()); } out.flush(); out.close(); } else { logger.warn("The folder name supplied is not a folder!"); System.err.println("The folder name supplied is not a folder!"); return null; } return zippedFile; }
From source file:org.rhq.plugins.jmx.test.JMXPluginTest.java
private static File createCustomPluginFile() throws Exception { File plugin = new File("src/test/resources/custom-test-plugin.xml"); File targetFile = new File(System.getProperty("java.io.tmpdir"), "custom-test-plugin.jar"); targetFile.deleteOnExit();//from ww w . j a v a 2 s. co m ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(targetFile)); ZipEntry metainf = new ZipEntry("META-INF"); outputStream.putNextEntry(metainf); outputStream.closeEntry(); ZipEntry pluginXml = new ZipEntry("META-INF/rhq-plugin.xml"); outputStream.putNextEntry(pluginXml); FileInputStream fis = new FileInputStream(plugin); int bufferSize = 1024; BufferedInputStream bufferedInputStream = new BufferedInputStream(fis, bufferSize); int size; byte data[] = new byte[bufferSize]; while ((size = bufferedInputStream.read(data, 0, bufferSize)) != -1) { outputStream.write(data, 0, size); } bufferedInputStream.close(); outputStream.closeEntry(); outputStream.flush(); outputStream.finish(); return targetFile; }
From source file:org.bonitasoft.engine.io.IOUtil.java
/** * Create a structured zip archive recursively. * The string must be OS specific String to represent path. * *//* w ww . j a va2s .c om*/ public static void zipDir(final String dir2zip, final ZipOutputStream zos, final String root) throws IOException { final File zipDir = new File(dir2zip); final byte[] readBuffer = new byte[BUFFER_SIZE]; for (final String pathName : zipDir.list()) { final File file = new File(zipDir, pathName); final String path = file.getPath(); if (file.isDirectory()) { zipDir(path, zos, root); continue; } try { final ZipEntry anEntry = new ZipEntry(path.substring(root.length() + 1, path.length()) .replace(String.valueOf(File.separatorChar), "/")); zos.putNextEntry(anEntry); copyFileToZip(zos, readBuffer, file); zos.flush(); } finally { zos.closeEntry(); } } }
From source file:au.org.ala.layers.grid.GridClassBuilder.java
public static HashMap<Integer, GridClass> buildFromGrid(String filePath) throws IOException { File wktDir = new File(filePath); wktDir.mkdirs();// w ww .j a v a 2s . com int[] wktMap = null; //track values for the SLD ArrayList<Integer> maxValues = new ArrayList<Integer>(); ArrayList<String> labels = new ArrayList<String>(); HashMap<Integer, GridClass> classes = new HashMap<Integer, GridClass>(); Properties p = new Properties(); p.load(new FileReader(filePath + ".txt")); boolean mergeProperties = false; Map<String, Set<Integer>> groupedKeys = new HashMap<String, Set<Integer>>(); Map<Integer, Integer> translateKeys = new HashMap<Integer, Integer>(); Map<String, Integer> translateValues = new HashMap<String, Integer>(); ArrayList<Integer> keys = new ArrayList<Integer>(); for (String key : p.stringPropertyNames()) { try { int k = Integer.parseInt(key); keys.add(k); //grouping of property file keys by value String value = p.getProperty(key); Set<Integer> klist = groupedKeys.get(value); if (klist == null) klist = new HashSet<Integer>(); else mergeProperties = true; klist.add(k); groupedKeys.put(value, klist); if (!translateValues.containsKey(value)) translateValues.put(value, translateValues.size() + 1); translateKeys.put(k, translateValues.get(value)); } catch (NumberFormatException e) { logger.info("Excluding shape key '" + key + "'"); } catch (Exception e) { logger.error(e.getMessage(), e); } } java.util.Collections.sort(keys); Grid g = new Grid(filePath); boolean generateWkt = false; //((long) g.nrows) * ((long) g.ncols) < (long) Integer.MAX_VALUE; if (mergeProperties) { g.replaceValues(translateKeys); if (!new File(filePath + ".txt.old").exists()) FileUtils.moveFile(new File(filePath + ".txt"), new File(filePath + ".txt.old")); StringBuilder sb = new StringBuilder(); for (String value : translateValues.keySet()) { sb.append(translateValues.get(value)).append("=").append(value).append('\n'); } FileUtils.writeStringToFile(new File(filePath + ".txt"), sb.toString()); return buildFromGrid(filePath); } if (generateWkt) { for (String name : groupedKeys.keySet()) { try { Set<Integer> klist = groupedKeys.get(name); String key = klist.iterator().next().toString(); int k = Integer.parseInt(key); GridClass gc = new GridClass(); gc.setName(name); gc.setId(k); if (klist.size() == 1) klist = null; logger.info("getting wkt for " + filePath + " > " + key); Map wktIndexed = Envelope.getGridSingleLayerEnvelopeAsWktIndexed( filePath + "," + key + "," + key, klist, wktMap); //write class wkt File zipFile = new File(filePath + File.separator + key + ".wkt.zip"); ZipOutputStream zos = null; try { zos = new ZipOutputStream(new FileOutputStream(zipFile)); zos.putNextEntry(new ZipEntry(key + ".wkt")); zos.write(((String) wktIndexed.get("wkt")).getBytes()); zos.flush(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (zos != null) { try { zos.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } BufferedOutputStream bos = null; try { bos = new BufferedOutputStream( new FileOutputStream(filePath + File.separator + key + ".wkt")); bos.write(((String) wktIndexed.get("wkt")).getBytes()); bos.flush(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (bos != null) { try { bos.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } logger.info("wkt written to file"); gc.setArea_km(SpatialUtil.calculateArea((String) wktIndexed.get("wkt")) / 1000.0 / 1000.0); //store map wktMap = (int[]) wktIndexed.get("map"); //write wkt index FileWriter fw = null; try { fw = new FileWriter(filePath + File.separator + key + ".wkt.index"); fw.append((String) wktIndexed.get("index")); fw.flush(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (fw != null) { try { fw.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } //write wkt index a binary, include extents (minx, miny, maxx, maxy) and area (sq km) int minPolygonNumber = 0; int maxPolygonNumber = 0; RandomAccessFile raf = null; try { raf = new RandomAccessFile(filePath + File.separator + key + ".wkt.index.dat", "rw"); String[] index = ((String) wktIndexed.get("index")).split("\n"); for (int i = 0; i < index.length; i++) { if (index[i].length() > 1) { String[] cells = index[i].split(","); int polygonNumber = Integer.parseInt(cells[0]); raf.writeInt(polygonNumber); //polygon number int polygonStart = Integer.parseInt(cells[1]); raf.writeInt(polygonStart); //character offset if (i == 0) { minPolygonNumber = polygonNumber; } else if (i == index.length - 1) { maxPolygonNumber = polygonNumber; } } } } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (raf != null) { try { raf.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } //for SLD maxValues.add(gc.getMaxShapeIdx()); labels.add(name.replace("\"", "'")); gc.setMinShapeIdx(minPolygonNumber); gc.setMaxShapeIdx(maxPolygonNumber); logger.info("getting multipolygon for " + filePath + " > " + key); MultiPolygon mp = Envelope.getGridEnvelopeAsMultiPolygon(filePath + "," + key + "," + key); gc.setBbox(mp.getEnvelope().toText().replace(" (", "(").replace(", ", ",")); classes.put(k, gc); try { //write class kml zos = null; try { zos = new ZipOutputStream( new FileOutputStream(filePath + File.separator + key + ".kml.zip")); zos.putNextEntry(new ZipEntry(key + ".kml")); Encoder encoder = new Encoder(new KMLConfiguration()); encoder.setIndenting(true); encoder.encode(mp, KML.Geometry, zos); zos.flush(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (zos != null) { try { zos.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } logger.info("kml written to file"); final SimpleFeatureType TYPE = DataUtilities.createType("class", "the_geom:MultiPolygon,id:Integer,name:String"); FeatureJSON fjson = new FeatureJSON(); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); SimpleFeature sf = featureBuilder.buildFeature(null); //write class geojson zos = null; try { zos = new ZipOutputStream( new FileOutputStream(filePath + File.separator + key + ".geojson.zip")); zos.putNextEntry(new ZipEntry(key + ".geojson")); featureBuilder.add(mp); featureBuilder.add(k); featureBuilder.add(name); fjson.writeFeature(sf, zos); zos.flush(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (zos != null) { try { zos.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } logger.info("geojson written to file"); //write class shape file File newFile = new File(filePath + File.separator + key + ".shp"); ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put("url", newFile.toURI().toURL()); params.put("create spatial index", Boolean.FALSE); ShapefileDataStore newDataStore = null; try { newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); newDataStore.createSchema(TYPE); newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); Transaction transaction = new DefaultTransaction("create"); String typeName = newDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName); SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; featureStore.setTransaction(transaction); List<SimpleFeature> features = new ArrayList<SimpleFeature>(); DefaultFeatureCollection collection = new DefaultFeatureCollection(); collection.addAll(features); featureStore.setTransaction(transaction); features.add(sf); featureStore.addFeatures(collection); transaction.commit(); transaction.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (newDataStore != null) { try { newDataStore.dispose(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } zos = null; try { zos = new ZipOutputStream( new FileOutputStream(filePath + File.separator + key + ".shp.zip")); //add .dbf .shp .shx .prj String[] exts = { ".dbf", ".shp", ".shx", ".prj" }; for (String ext : exts) { zos.putNextEntry(new ZipEntry(key + ext)); FileInputStream fis = null; try { fis = new FileInputStream(filePath + File.separator + key + ext); byte[] buffer = new byte[1024]; int size; while ((size = fis.read(buffer)) > 0) { zos.write(buffer, 0, size); } } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (fis != null) { try { fis.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } //remove unzipped files new File(filePath + File.separator + key + ext).delete(); } zos.flush(); } catch (Exception e) { logger.error(e.getMessage(), e); } finally { if (zos != null) { try { zos.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } logger.info("shape file written to zip"); } catch (Exception e) { logger.error(e.getMessage(), e); } } catch (Exception e) { logger.error(e.getMessage(), e); } } //write polygon mapping g.writeGrid(filePath + File.separator + "polygons", wktMap, g.xmin, g.ymin, g.xmax, g.ymax, g.xres, g.yres, g.nrows, g.ncols); //copy the header file to get it exactly the same, but change the data type copyHeaderAsInt(filePath + ".grd", filePath + File.separator + "polygons.grd"); } else { //build classes without generating polygons Map<Float, float[]> info = new HashMap<Float, float[]>(); for (int j = 0; j < keys.size(); j++) { info.put(keys.get(j).floatValue(), new float[] { 0, Float.NaN, Float.NaN, Float.NaN, Float.NaN }); } g.getClassInfo(info); for (int j = 0; j < keys.size(); j++) { int k = keys.get(j); String key = String.valueOf(k); String name = p.getProperty(key); GridClass gc = new GridClass(); gc.setName(name); gc.setId(k); //for SLD maxValues.add(Integer.valueOf(key)); labels.add(name.replace("\"", "'")); gc.setMinShapeIdx(Integer.valueOf(key)); gc.setMaxShapeIdx(Integer.valueOf(key)); float[] stats = info.get(keys.get(j).floatValue()); //only include if area > 0 if (stats[0] > 0) { gc.setBbox("POLYGON((" + stats[1] + " " + stats[2] + "," + stats[1] + " " + stats[4] + "," + stats[3] + " " + stats[4] + "," + stats[3] + " " + stats[2] + "," + stats[1] + " " + stats[2] + "))"); gc.setArea_km((double) stats[0]); classes.put(k, gc); } } } //write sld exportSLD(filePath + File.separator + "polygons.sld", new File(filePath + ".txt").getName(), maxValues, labels); writeProjectionFile(filePath + File.separator + "polygons.prj"); //write .classes.json ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(new File(filePath + ".classes.json"), classes); return classes; }
From source file:org.wso2.carbon.identity.user.store.ws.util.FileUtil.java
/** * Zip directory//from w w w . j av a 2 s. c om * @param srcFolder * @param destinationZipFile * @throws Exception */ public void zipDirectory(String srcFolder, String destinationZipFile) throws IOException { FileOutputStream fileWriter = new FileOutputStream(destinationZipFile); ZipOutputStream zip = new ZipOutputStream(fileWriter); addFolderToZip("", srcFolder, zip); zip.flush(); zip.close(); }
From source file:net.grinder.util.LogCompressUtil.java
/** * Compress multiple Files./*www .j av a 2 s.c o 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); } }
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 va 2 s .com*/ */ 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:org.sakaiproject.search.util.FileUtils.java
/** * pack a segment into the zip/*from ww w. j a va 2 s. c om*/ * @param compress * * @param addsi * @return * @throws IOException */ public static void pack(File source, final String basePath, final String replacePath, OutputStream output, boolean compress) throws IOException { log.debug("Packing " + source + " repacing " + basePath + " with " + replacePath); final ZipOutputStream zout = new ZipOutputStream(output); if (compress) { zout.setLevel(ZipOutputStream.DEFLATED); } else { zout.setLevel(ZipOutputStream.STORED); } final byte[] buffer = new byte[1024 * 100]; try { recurse(source, new RecurseAction() { public void doFile(File file) throws IOException { if (!file.isDirectory()) { log.debug(" Add " + file.getPath()); addSingleFile(basePath, replacePath, file, zout, buffer); } else { log.debug(" Ignore " + file.getPath()); } } public void doBeforeFile(File f) { } public void doAfterFile(File f) { } }); } finally { zout.flush(); try { zout.close(); } catch (Exception e) { log.warn("Exception closing output zip", e); } } }