Java Code Examples for java.util.zip.ZipOutputStream#setLevel()
The following examples show how to use
java.util.zip.ZipOutputStream#setLevel() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompressBitmap.java From mars-sim with GNU General Public License v3.0 | 6 votes |
public static void zipStats(String filename) throws IOException { Path path = Paths.get(filename); byte[] input = Files.readAllBytes(path); System.out.println("I will try to compress the original bitmap using zip."); long bef = System.nanoTime(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); zos.setLevel(9); ZipEntry entry = new ZipEntry(filename); entry.setSize(input.length); zos.putNextEntry(entry); zos.write(input); zos.closeEntry(); zos.close(); byte[] result = baos.toByteArray(); long aft = System.nanoTime(); System.out.println("zip encoding speed:"+input.length*1000.0/(aft-bef)+" million of bytes per second"); System.out.println("zip compression ratio at best level : "+input.length * 1.0 / result.length); }
Example 2
Source File: ZIPSerializer.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 6 votes |
public void writeObject(ByteBuffer buffer, Object object) throws IOException { if (!(object instanceof ZIPCompressedMessage)) return; ZIPCompressedMessage zipMessage = (ZIPCompressedMessage)object; Message message = zipMessage.getMessage(); ByteBuffer tempBuffer = ByteBuffer.allocate(512000); Serializer.writeClassAndObject(tempBuffer, message); ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream(); ZipOutputStream zipOutput = new ZipOutputStream(byteArrayOutput); zipOutput.setLevel(zipMessage.getLevel()); ZipEntry zipEntry = new ZipEntry("zip"); zipOutput.putNextEntry(zipEntry); zipOutput.write(tempBuffer.array()); zipOutput.flush(); zipOutput.closeEntry(); zipOutput.close(); buffer.put(byteArrayOutput.toByteArray()); }
Example 3
Source File: ZipUtils.java From bladecoder-adventure-engine with Apache License 2.0 | 6 votes |
public static void packZip(List<File> sources, File output) throws IOException { EditorLogger.debug("Packaging to " + output.getName()); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(output)); zipOut.setLevel(Deflater.DEFAULT_COMPRESSION); for (File source : sources) { if (source.isDirectory()) { zipDir(zipOut, "", source); } else { zipFile(zipOut, "", source); } } zipOut.flush(); zipOut.close(); EditorLogger.debug("Done"); }
Example 4
Source File: MediaController.java From htwplus with MIT License | 6 votes |
private File createZIP(List<Media> media) throws IOException { //cleanUpTemp(); // JUST FOR DEVELOPMENT, DO NOT USE IN PRODUCTION String tmpPath = configuration.getString("media.tempPath"); File file = File.createTempFile(tempPrefix, ".tmp", new File(tmpPath)); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(file)); zipOut.setLevel(Deflater.NO_COMPRESSION); byte[] buffer = new byte[4092]; int byteCount = 0; for (Media m : media) { zipOut.putNextEntry(new ZipEntry(m.fileName)); FileInputStream fis = new FileInputStream(m.file); byteCount = 0; while ((byteCount = fis.read(buffer)) != -1) { zipOut.write(buffer, 0, byteCount); } fis.close(); zipOut.closeEntry(); } zipOut.flush(); zipOut.close(); return file; }
Example 5
Source File: ZipBytes.java From signer with GNU Lesser General Public License v3.0 | 6 votes |
/** * * @param files files to compress * @return compressed bundle */ public static byte[] compressing(Map<String, byte[]> files) { ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zipOut = new ZipOutputStream(out); try { for (String fileName : files.keySet()) { LOGGER.log(Level.INFO, coreMessagesBundle.getString("info.add.file.zip",fileName)); zipOut.putNextEntry(new ZipEntry(fileName)); zipOut.write(files.get(fileName)); zipOut.setLevel(0); zipOut.closeEntry(); } zipOut.close(); out.close(); } catch (IOException e) { new CertificateUtilException(e.getMessage(), e); } return out.toByteArray(); }
Example 6
Source File: TestScriptsBean.java From sailfish-core with Apache License 2.0 | 5 votes |
private String packZip() throws IOException { Map<String, Integer> nameCounter = new HashMap<>(); File temp = File.createTempFile("matrix.zip", Long.toString(System.currentTimeMillis())); String path = temp.getAbsolutePath(); ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(temp)); zip.setLevel(Deflater.DEFAULT_COMPRESSION); byte[] buf = new byte[1024]; for (Long matrixId : selectedMatrices) { MatrixAdapter matrixAdapter = getMatrixAdapterById(matrixId); String name = matrixAdapter.getName(); if (nameCounter.containsKey(name)) { Integer count = nameCounter.get(name); count++; nameCounter.put(name, count); name = "(" + count + ") " + name; } else { nameCounter.put(name, 0); } zip.putNextEntry(new ZipEntry(name)); try (InputStream fis = matrixAdapter.readStream()) { int len; while ((len = fis.read(buf)) > 0) { zip.write(buf, 0, len); } } zip.closeEntry(); } zip.flush(); zip.close(); return path; }
Example 7
Source File: FileUtil.java From scipio-erp with Apache License 2.0 | 5 votes |
/** * For an inputStream and a file name, create a zip stream containing only one entry with the inputStream set to fileName * If fileName is empty, generate a unique one. * * @param fileStream * @param fileName * @return * @throws IOException */ public static ByteArrayInputStream zipFileStream(InputStream fileStream, String fileName) throws IOException { if (fileStream == null) return null; if (fileName == null) fileName = UUID.randomUUID().toString(); // Create zip file from content input stream String zipFileName = UUID.randomUUID().toString() + ".zip"; String zipFilePath = UtilProperties.getPropertyValue("general", "http.upload.tmprepository", "runtime/tmp"); FileOutputStream fos = new FileOutputStream(new File(zipFilePath, zipFileName)); ZipOutputStream zos = new ZipOutputStream(fos); zos.setMethod(ZipOutputStream.DEFLATED); zos.setLevel(Deflater.BEST_COMPRESSION); ZipEntry ze = new ZipEntry(fileName); zos.putNextEntry(ze); int len; byte[] bufferData = new byte[8192]; while ((len = fileStream.read(bufferData)) > 0) { zos.write(bufferData, 0, len); } zos.closeEntry(); zos.close(); fos.close(); //prepare zip stream File tmpZipFile = new File(zipFilePath, zipFileName); ByteArrayInputStream zipStream = new ByteArrayInputStream(FileUtils.readFileToByteArray(tmpZipFile)); //Then delete zip file tmpZipFile.delete(); return zipStream; }
Example 8
Source File: OdsArchiveExplorerTest.java From fastods with GNU General Public License v3.0 | 5 votes |
private byte[] createArchiveAsBytes() throws IOException { final ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); final ZipOutputStream zos = new ZipOutputStream(bos); zos.setMethod(ZipOutputStream.DEFLATED); zos.setLevel(0); zos.putNextEntry(new ZipEntry("temp1")); zos.write(1); zos.putNextEntry(new ZipEntry(ManifestElement.META_INF_MANIFEST_XML)); zos.write(MANIFEST.getBytes(CharsetUtil.UTF_8)); zos.putNextEntry(new ZipEntry("temp2")); zos.write(2); zos.finish(); zos.close(); return bos.toByteArray(); }
Example 9
Source File: PublishAssetPackAction.java From MikuMikuStudio with BSD 2-Clause "Simplified" License | 5 votes |
private void packZip(WizardDescriptor wiz) { try { String outFilename = context.getProjectDirectory().getPath() + "/" + wiz.getProperty("filename"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(new File(outFilename))); out.setLevel(9); zipDir(((AssetPackProject) context).getProjectDirectory(), out, (String) wiz.getProperty("filename")); out.close(); } catch (IOException e) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Error creating ZIP file!"); } }
Example 10
Source File: UnoPkg.java From sis with Apache License 2.0 | 5 votes |
/** * Copies a JAR file in the given ZIP file, but without compression for the files * in {@code SIS_DATA} directory. * * @param file the JAR file to copy. * @param bundle destination where to copy the JAR file. */ private static void copyInflated(final File file, final ZipOutputStream bundle) throws IOException { final ZipEntry entry = new ZipEntry(file.getName()); bundle.putNextEntry(entry); final ZipOutputStream out = new ZipOutputStream(bundle); out.setLevel(9); try (ZipFile in = new ZipFile(file)) { final Enumeration<? extends ZipEntry> entries = in.entries(); while (entries.hasMoreElements()) { final ZipEntry inEntry = entries.nextElement(); final String name = inEntry.getName(); final ZipEntry outEntry = new ZipEntry(name); if (name.startsWith("SIS_DATA")) { final long size = inEntry.getSize(); outEntry.setMethod(ZipOutputStream.STORED); outEntry.setSize(size); outEntry.setCompressedSize(size); outEntry.setCrc(inEntry.getCrc()); } try (InputStream inStream = in.getInputStream(inEntry)) { out.putNextEntry(outEntry); inStream.transferTo(out); out.closeEntry(); } } } out.finish(); bundle.closeEntry(); }
Example 11
Source File: AbstractASiCSignatureService.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
private void storeSignedFiles(final List<DSSDocument> detachedDocuments, final ZipOutputStream zos) throws IOException { for (DSSDocument detachedDocument : detachedDocuments) { try (InputStream is = detachedDocument.openStream()) { final String detachedDocumentName = detachedDocument.getName(); final String name = detachedDocumentName != null ? detachedDocumentName : ASiCUtils.ZIP_ENTRY_DETACHED_FILE; final ZipEntry entryDocument = new ZipEntry(name); zos.setLevel(ZipEntry.DEFLATED); zos.putNextEntry(entryDocument); Utils.copy(is, zos); } } }
Example 12
Source File: FileSystemDataStore.java From DataHubSystem with GNU Affero General Public License v3.0 | 5 votes |
/** * Generates a zip file. * * @param source source file or directory to compress. * @param destination destination of zipped file. * @return the zipped file. */ private void generateZip (File source, File destination) throws IOException { if (source == null || !source.exists ()) { throw new IllegalArgumentException ("source file should exist"); } if (destination == null) { throw new IllegalArgumentException ( "destination file should be not null"); } FileOutputStream output = new FileOutputStream (destination); ZipOutputStream zip_out = new ZipOutputStream (output); zip_out.setLevel ( cfgManager.getDownloadConfiguration ().getCompressionLevel ()); List<QualifiedFile> file_list = getFileList (source); byte[] buffer = new byte[BUFFER_SIZE]; for (QualifiedFile qualified_file : file_list) { ZipEntry entry = new ZipEntry (qualified_file.getQualifier ()); InputStream input = new FileInputStream (qualified_file.getFile ()); int read; zip_out.putNextEntry (entry); while ((read = input.read (buffer)) != -1) { zip_out.write (buffer, 0, read); } input.close (); zip_out.closeEntry (); } zip_out.close (); output.close (); }
Example 13
Source File: ZipUtils.java From scheduling with GNU Affero General Public License v3.0 | 5 votes |
/** * Create a zip file that contains all the directory listed in directories parameter. * @param zos output stream destination * @param directoriesAndFiles the list of directories and files to be zipped. * @param crc the CRC32 of all zipentries. Can be null if no crc is needed. * @throws IOException if an error occurs during the compression */ public static void zipDirectoriesAndFiles(ZipOutputStream zos, String[] directoriesAndFiles, CRC32 crc) throws IOException { // Create zip stream zos.setLevel(COMP_LEVEL); // zip file is ready zipIt(zos, directoriesAndFiles, crc); // Close the file output streams zos.flush(); }
Example 14
Source File: LoginServer.java From aion-germany with GNU General Public License v3.0 | 4 votes |
private static void initalizeLoggger() { new File("./log/backup/").mkdirs(); File[] files = new File("log").listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".log"); } }); if (files != null && files.length > 0) { byte[] buf = new byte[1024]; try { String outFilename = "./log/backup/" + new SimpleDateFormat("yyyy-MM-dd HHmmss").format(new Date()) + ".zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); out.setMethod(ZipOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); for (File logFile : files) { FileInputStream in = new FileInputStream(logFile); out.putNextEntry(new ZipEntry(logFile.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); logFile.delete(); } out.close(); } catch (IOException e) { } } LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); try { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); configurator.doConfigure("config/slf4j-logback.xml"); } catch (JoranException je) { throw new RuntimeException("Failed to configure loggers, shutting down...", je); } }
Example 15
Source File: SpringBootZipOnDemandInputStream.java From camel-spring-boot with Apache License 2.0 | 4 votes |
protected ZipOutputStream createOutputStream(final OutputStream outputStream) { ZipOutputStream stream = new ZipOutputStream(outputStream); stream.setMethod(ZipEntry.STORED); stream.setLevel(Deflater.NO_COMPRESSION); return stream; }
Example 16
Source File: GameServer.java From aion-germany with GNU General Public License v3.0 | 4 votes |
private static void initalizeLoggger() { new File("./log/backup/").mkdirs(); File[] files = new File("log").listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".log"); } }); if (files != null && files.length > 0) { byte[] buf = new byte[1024]; try { String outFilename = "./log/backup/" + new SimpleDateFormat("yyyy-MM-dd HHmmss").format(new Date()) + ".zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); out.setMethod(ZipOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); for (File logFile : files) { FileInputStream in = new FileInputStream(logFile); out.putNextEntry(new ZipEntry(logFile.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); logFile.delete(); } out.close(); } catch (IOException e) { e.printStackTrace(); } } LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); try { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); configurator.doConfigure("config/slf4j-logback.xml"); } catch (JoranException je) { throw new RuntimeException("[LoggerFactory] Failed to configure loggers, shutting down...", je); } }
Example 17
Source File: YarnRemoteInterpreterProcess.java From zeppelin with Apache License 2.0 | 4 votes |
/** * * Create zip file to interpreter. * The contents are all the stuff under ZEPPELIN_HOME/interpreter/{interpreter_name} * @return * @throws IOException */ private File createInterpreterZip() throws IOException { File interpreterArchive = File.createTempFile("zeppelin_interpreter_", ".zip", Files.createTempDir()); ZipOutputStream interpreterZipStream = new ZipOutputStream(new FileOutputStream(interpreterArchive)); interpreterZipStream.setLevel(0); String zeppelinHomeEnv = System.getenv("ZEPPELIN_HOME"); if (org.apache.commons.lang3.StringUtils.isBlank(zeppelinHomeEnv)) { throw new IOException("ZEPPELIN_HOME is not specified"); } File zeppelinHome = new File(zeppelinHomeEnv); File binDir = new File(zeppelinHome, "bin"); addFileToZipStream(interpreterZipStream, binDir, null); File confDir = new File(zeppelinHome, "conf"); addFileToZipStream(interpreterZipStream, confDir, null); File interpreterDir = new File(zeppelinHome, "interpreter/" + launchContext.getInterpreterSettingGroup()); addFileToZipStream(interpreterZipStream, interpreterDir, "interpreter"); File localRepoDir = new File(zConf.getInterpreterLocalRepoPath() + "/" + launchContext.getInterpreterSettingName()); if (localRepoDir.exists() && localRepoDir.isDirectory()) { LOGGER.debug("Adding localRepoDir {} to interpreter zip: ", localRepoDir.getAbsolutePath()); addFileToZipStream(interpreterZipStream, localRepoDir, "local-repo"); } // add zeppelin-interpreter-shaded jar File[] interpreterShadedFiles = new File(zeppelinHome, "interpreter").listFiles( file -> file.getName().startsWith("zeppelin-interpreter-shaded") && file.getName().endsWith(".jar")); if (interpreterShadedFiles.length == 0) { throw new IOException("No zeppelin-interpreter-shaded jar found under " + zeppelinHome.getAbsolutePath() + "/interpreter"); } if (interpreterShadedFiles.length > 1) { throw new IOException("More than 1 zeppelin-interpreter-shaded jars found under " + zeppelinHome.getAbsolutePath() + "/interpreter"); } addFileToZipStream(interpreterZipStream, interpreterShadedFiles[0], "interpreter"); interpreterZipStream.flush(); interpreterZipStream.close(); return interpreterArchive; }
Example 18
Source File: ChatServer.java From aion-germany with GNU General Public License v3.0 | 4 votes |
private static void initalizeLoggger() { new File("./log/backup/").mkdirs(); File[] files = new File("log").listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.endsWith(".log"); } }); if (files != null && files.length > 0) { byte[] buf = new byte[1024]; try { String outFilename = "./log/backup/" + new SimpleDateFormat("yyyy-MM-dd HHmmss").format(new Date()) + ".zip"; ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); out.setMethod(ZipOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); for (File logFile : files) { FileInputStream in = new FileInputStream(logFile); out.putNextEntry(new ZipEntry(logFile.getName())); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); logFile.delete(); } out.close(); } catch (IOException e) { } } LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); try { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); configurator.doConfigure("config/slf4j-logback.xml"); } catch (JoranException je) { throw new RuntimeException("Failed to configure loggers, shutting down...", je); } }
Example 19
Source File: Assembler.java From sis with Apache License 2.0 | 4 votes |
/** * Copies a JAR file in the given ZIP file, except the native resources which are stored in the given map. * * @param file the JAR file to copy. * @param bundle destination where to copy the JAR file. * @param nativeFiles where to store the native resources. */ private void appendJAR(final File file, final ZipArchiveOutputStream bundle, final Map<String,byte[]> nativeFiles) throws IOException { try (ZipFile in = new ZipFile(file)) { if (hasNativeResources(in)) { final ZipOutputStream out = new ZipOutputStream(bundle); out.setLevel(9); final Enumeration<? extends ZipEntry> entries = in.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); final String name = entry.getName(); try (InputStream eis = in.getInputStream(entry)) { if (!name.startsWith(NATIVE_DIRECTORY)) { out.putNextEntry(new ZipEntry(name)); eis.transferTo(out); // Copy the entry verbatim. out.closeEntry(); } else if (!entry.isDirectory()) { final long size = entry.getSize(); // For memorizing the entry without copying it now. if (size <= 0 || size > Integer.MAX_VALUE) { throw new IOException(String.format("Errors while copying %s:%n" + "Unsupported size for \"%s\" entry: %d", file, name, size)); } final byte[] content = new byte[(int) size]; final int actual = eis.read(content); if (actual != size) { throw new IOException(String.format("Errors while copying %s:%n" + "Expected %d bytes in \"%s\" but found %d", file, size, name, actual)); } if (nativeFiles.put(name.substring(NATIVE_DIRECTORY.length()), content) != null) { throw new IOException(String.format("Errors while copying %s:%n" + "Duplicated entry: %s", file, name)); } } } } out.finish(); return; } } /* * If the JAR file has no native resources to exclude, we can copy the JAR file verbatim. * This is faster than inflating and deflating again the JAR file. */ try (FileInputStream in = new FileInputStream(file)) { in.transferTo(bundle); } }
Example 20
Source File: ZipUtils.java From che with Eclipse Public License 2.0 | 3 votes |
/** * Creates a {@link ZipOutputStream} with proper buffering and options * * @param zip * @return a newly opened stream * @throws FileNotFoundException */ public static ZipOutputStream stream(Path zip) throws FileNotFoundException { ZipOutputStream result = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zip.toFile()))); result.setLevel(0); return result; }