java.util.zip.ZipOutputStream#setLevel ( )源码实例Demo

下面列出了java.util.zip.ZipOutputStream#setLevel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: signer   文件: ZipBytes.java
/**
 * 
 * @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();
}
 
源代码2 项目: mars-sim   文件: CompressBitmap.java
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);
}
 
源代码3 项目: htwplus   文件: MediaController.java
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;
    }
 
源代码4 项目: bladecoder-adventure-engine   文件: ZipUtils.java
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");
}
 
源代码5 项目: MikuMikuStudio   文件: ZIPSerializer.java
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());
}
 
源代码6 项目: sailfish-core   文件: TestScriptsBean.java
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;

	}
 
源代码7 项目: scipio-erp   文件: FileUtil.java
/**
 * 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;
}
 
源代码8 项目: fastods   文件: OdsArchiveExplorerTest.java
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();
}
 
源代码9 项目: MikuMikuStudio   文件: PublishAssetPackAction.java
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!");
    }
}
 
源代码10 项目: sis   文件: UnoPkg.java
/**
 * 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();
}
 
源代码11 项目: dss   文件: AbstractASiCSignatureService.java
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);
		}
	}
}
 
源代码12 项目: DataHubSystem   文件: FileSystemDataStore.java
/**
 * 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 ();
}
 
源代码13 项目: scheduling   文件: ZipUtils.java
/**
 * 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();
}
 
protected ZipOutputStream createOutputStream(final OutputStream outputStream) {
    ZipOutputStream stream = new ZipOutputStream(outputStream);
    stream.setMethod(ZipEntry.STORED);
    stream.setLevel(Deflater.NO_COMPRESSION);
    return stream;
}
 
源代码15 项目: sis   文件: Assembler.java
/**
 * 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);
    }
}
 
源代码16 项目: aion-germany   文件: ChatServer.java
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);
    }
}
 
源代码17 项目: aion-germany   文件: GameServer.java
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);
	}
}
 
源代码18 项目: aion-germany   文件: LoginServer.java
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);
    }
}
 
源代码19 项目: zeppelin   文件: YarnRemoteInterpreterProcess.java
/**
 *
 * 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;
}
 
源代码20 项目: che   文件: ZipUtils.java
/**
 * 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;
}