获得 {@link ZipOutputStream}

@param zipFile 压缩文件
@param charset 编码
@return {@link ZipOutputStream}
540686
private static ZipOutputStream getZipOutputStream(OutputStream out, Charset charset) {
		charset = (null == charset) ? DEFAULT_CHARSET : charset;
		return new ZipOutputStream(out, charset);
	}
179727
public static void copyZipWithoutEmptyDirectories(final File inputFile, final File outputFile) throws IOException
    {
        final byte[] buf = new byte[0x2000];

        final ZipFile inputZip = new ZipFile(inputFile);
        final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(outputFile));
        try
        {
            // read a the entries of the input zip file and sort them
            final Enumeration<? extends ZipEntry> e = inputZip.entries();
            final ArrayList<ZipEntry> sortedList = new ArrayList<ZipEntry>();
            while (e.hasMoreElements()) {
                final ZipEntry entry = e.nextElement();
                sortedList.add(entry);
            }

            Collections.sort(sortedList, new Comparator<ZipEntry>()
            {
                public int compare(ZipEntry o1, ZipEntry o2)
                {
                    String n1 = o1.getName(), n2 = o2.getName();
                    if (metaOverride(n1, n2)) {
                        return -1;
                    }
                    if (metaOverride(n2, n1)) {
                        return 1;
                    }
                    return n1.compareTo(n2);
                }

                // make sure that META-INF/MANIFEST.MF is always the first entry after META-INF/
                private boolean metaOverride(String n1, String n2) {
                    return (n1.startsWith("META-INF/") && !n2.startsWith("META-INF/"))
                        || (n1.equals("META-INF/MANIFEST.MF") && !n2.equals(n1) && !n2.equals("META-INF/"))
                        || (n1.equals("META-INF/") && !n2.equals(n1));
                }
            });

            // treat them again and write them in output, wenn they not are empty directories
            for (int i = sortedList.size()-1; i>=0; i--)
            {
                final ZipEntry inputEntry = sortedList.get(i);
                final String name = inputEntry.getName();
                final boolean isEmptyDirectory;
                if (inputEntry.isDirectory())
                {
                    if (i == sortedList.size()-1)
                    {
                        // no item afterwards; it was an empty directory
                        isEmptyDirectory = true;
                    }
                    else
                    {
                        final String nextName = sortedList.get(i+1).getName();
                        isEmptyDirectory  = !nextName.startsWith(name);
                    }
                }
                else
                {
                    isEmptyDirectory = false;
                }

                if (isEmptyDirectory)
                {
                    sortedList.remove(i);
                }
            }

            // finally write entries in normal order
            for (int i = 0; i < sortedList.size(); i++)
            {
                final ZipEntry inputEntry = sortedList.get(i);
                final ZipEntry outputEntry = new ZipEntry(inputEntry);
                outputStream.putNextEntry(outputEntry);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                final InputStream is = inputZip.getInputStream(inputEntry);
                IoUtil.pipe(is, baos, buf);
                is.close();
                outputStream.write(baos.toByteArray());
            }
        } finally {
            outputStream.close();
            inputZip.close();
        }

    }
861805
public ZipUTF8Writer build(final OutputStream out) {
		final OutputStream bufferedOut;
		switch (this.zipBufferSize) {
		case NO_BUFFER:
			bufferedOut = out;
			break;
		case DEFAULT_BUFFER:
			bufferedOut = new BufferedOutputStream(out);
			break;
		default:
			bufferedOut = new BufferedOutputStream(out, this.zipBufferSize);
			break;
		}
		final ZipOutputStream zipOut = new ZipOutputStream(bufferedOut);
		zipOut.setMethod(ZipOutputStream.DEFLATED);
		zipOut.setLevel(this.level);
		final Writer writer = new OutputStreamWriter(zipOut,
				ZipUTF8Writer.UTF_8);
		final Writer bufferedWriter;
		switch (this.writerBufferSize) {
		case NO_BUFFER:
			bufferedWriter = writer;
			break;
		case DEFAULT_BUFFER:
			bufferedWriter = new BufferedWriter(writer);
			break;
		default:
			bufferedWriter = new BufferedWriter(writer, this.writerBufferSize);
			break;
		}
		return new ZipUTF8WriterImpl(zipOut, bufferedWriter);
	}
791132
public static void extract(final File zipfile, final File outdir) {
        try {
            ZipInputStream zin = new ZipInputStream(new FileInputStream(zipfile));
            ZipEntry entry;
            String name, dir;
            while ((entry = zin.getNextEntry()) != null) {
                name = entry.getName();
                if (entry.isDirectory()) {
                    mkdirs(outdir, name);
                    continue;
                }

                dir = dirpart(name);
                if (dir != null) {
                    mkdirs(outdir, dir);
                }

                extractFile(zin, outdir, name);
            }
            zin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
109051
public static File zip(List<String> fileNames, String zipPath, String encoding) {
    try {
      FileOutputStream f = new FileOutputStream(zipPath);
      ZipArchiveOutputStream zos = (ZipArchiveOutputStream) new ArchiveStreamFactory()
          .createArchiveOutputStream(ArchiveStreamFactory.ZIP, f);
      if (null != encoding) {
        zos.setEncoding(encoding);
      }
      for (int i = 0; i < fileNames.size(); i++) {
        String fileName = fileNames.get(i);
        String entryName = Strings.substringAfterLast(fileName, File.separator);
        ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
        zos.putArchiveEntry(entry);
        FileInputStream fis = new FileInputStream(fileName);
        IOs.copy(fis, zos);
        fis.close();
        zos.closeArchiveEntry();
      }
      zos.close();
      return new File(zipPath);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
1209484
private void unzip(final File file, final String outputDir) throws IOException {

		try (final ZipFile zipFile = new ZipFile(file)) {

			final Enumeration<? extends ZipEntry> entries = zipFile.entries();

			while (entries.hasMoreElements()) {

				final ZipEntry entry = entries.nextElement();
				final File targetFile = new File(outputDir, entry.getName());

				if (entry.isDirectory()) {

					targetFile.mkdirs();

				} else {

					targetFile.getParentFile().mkdirs();
					InputStream in = zipFile.getInputStream(entry);

					try (OutputStream out = new FileOutputStream(targetFile)) {

						IOUtils.copy(in, out);
						IOUtils.closeQuietly(in);
					}
				}
			}
		}
	}
114522
public static void unzip(String jarFile, Path destination) throws IOException {
        Map<String, String> zipProperties = new HashMap<>();
        /* We want to read an existing ZIP File, so we set this to false */
        zipProperties.put("create", "false");
        zipProperties.put("encoding", "UTF-8");
        URI zipFile = URI.create("jar:" + jarFile);

        try (FileSystem zipfs = FileSystems.newFileSystem(zipFile, zipProperties)) {
            Path rootPath = zipfs.getPath("/");
            Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    Path targetPath = destination.resolve(rootPath.relativize(dir).toString());
                    if (!Files.exists(targetPath)) {
                        Files.createDirectory(targetPath);
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Files.copy(file, destination.resolve(rootPath.relativize(file).toString()), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
312351
public static void main (String[] args) throws UnsupportedEncodingException
    {
        byte[] bytes = encode(args[0].getBytes(ZIP_CHARSET));
          System.out.println(new String(bytes, ZIP_CHARSET));
    }
22946
static ZipOutputStream createZipOutputStream(BufferedOutputStream outStream, Charset charset) {
    if (charset == null)
      return new ZipOutputStream(outStream);

    try {
      Constructor<ZipOutputStream> constructor = ZipOutputStream.class.getConstructor(new Class[] { OutputStream.class, Charset.class });
      return (ZipOutputStream) constructor.newInstance(new Object[] { outStream, charset });
    }
    catch (NoSuchMethodException e) {
      throw new IllegalStateException(MISSING_METHOD_PLEASE_UPGRADE, e);
    }
    catch (InstantiationException e) {
      throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_OUTPUT + e.getMessage(), e);
    }
    catch (IllegalAccessException e) {
      throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_OUTPUT + e.getMessage(), e);
    }
    catch (IllegalArgumentException e) {
      throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_OUTPUT + e.getMessage(), e);
    }
    catch (InvocationTargetException e) {
      throw new IllegalStateException(CONSTRUCTOR_MESSAGE_FOR_OUTPUT + e.getMessage(), e);
    }
  }
961840
public static void mapZipFile(final byte[] zipFile, final Map<String, byte[]> output, final String keyPrefix,
            final String keyUnPrefix) {
        try {
            final String fixedKeyPrefix = keyPrefix == null ? "" : keyPrefix;
            final String fixedKeyUnPrefix = keyUnPrefix == null ? "" : keyUnPrefix;
            final ZipInputStream zf = new ZipInputStream(new ByteArrayInputStream(zipFile));

            ZipEntry ze = null;
            while ((ze = zf.getNextEntry()) != null) {
                final String name = ze.getName();
                final long size = ze.getSize();

                // see if we are working with a file or a directory
                if (size != 0) {
                    byte[] fileContents = new byte[0];
                    final byte[] fileBuffer = new byte[BUFFER_SIZE];

                    int bytesRead = 0;
                    while ((bytesRead = zf.read(fileBuffer, 0, BUFFER_SIZE)) != -1) fileContents = ArrayUtils.addAll(fileContents,
                            bytesRead == BUFFER_SIZE ? fileBuffer : ArrayUtils.subarray(fileBuffer, 0, bytesRead));

                    output.put(fixedKeyPrefix + name.replace(fixedKeyUnPrefix, ""), fileContents);
                }
            }

        } catch (final IOException ex) {
           LOG.error("Unable to read file contents", ex);
        }
    }
373744
public static void zipDir(File zipFile, File dirObj) throws Exception {
        // File dirObj = new File(dir);
        if (!dirObj.isDirectory()) {
            System.err.println(dirObj.getName() + " is not a directory");
            System.exit(1);
        }

        try {

            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                    zipFile));

            System.out.println("Creating : " + zipFile);

            addDir(dirObj, out);
            // Complete the ZIP file
            out.close();

        } catch (IOException e) {
            throw new Exception(e.getMessage());
        }

    }
1204197
public static File zip(String srcPath, String zipPath, boolean withSrcDir) throws UtilException {
		return zip(srcPath, zipPath, DEFAULT_CHARSET, withSrcDir);
	}
653088
public static void zipFiles(List<File> files, OutputStream outputStream) throws IOException {
        ZipOutputStream zos = new ZipOutputStream(outputStream);

        for (File file : files) {
            if (file.isDirectory()) {   //if it's a folder
                addFolderToZip("", file, zos);
            } else {
                addFileToZip("", file, zos);
            }
        }

        zos.finish();
    }
304165
public static File zip(String srcPath, String zipPath, Charset charset, boolean withSrcDir) throws UtilException {
		final File srcFile = FileUtil.file(srcPath);
		final File zipFile = FileUtil.file(zipPath);
		zip(zipFile, charset, withSrcDir, srcFile);
		return zipFile;
	}
374428
public static void compress(String srcFilePath, String destFilePath) throws Exception {
		File src = new File(srcFilePath);
		if (!src.exists()) {
			throw new RuntimeException(srcFilePath + "不存在");
		}
		File zipFile = new File(destFilePath);
		ZipOutputStream zos=null;
		try {
			FileOutputStream fos = new FileOutputStream(zipFile);
			CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
			zos = new ZipOutputStream(cos);
			String baseDir = "";
			compressbyType(src, zos, baseDir);
		}finally {
			if(zos!=null)
			{
				zos.close();
			}
		}
	}
990079
public static String readStringForZip(String zipFilePathOrClasspath,
            String entryName, String charsetName) {
        return JMInputStream.toString(
                getResourceInputStreamForZip(zipFilePathOrClasspath, entryName),
                charsetName);
    }
861845
@Override
    @Trivial
    public ZipFile open() throws IOException {
        String methodName = "open";

        synchronized( zipFileLock ) {
            if ( zipFile == null ) {
                debug(methodName, "Opening");
                if ( zipFileReaper == null ) {
                    zipFile = ZipFileUtils.openZipFile(file); // throws IOException
                } else {
                    zipFile = zipFileReaper.open(path);
                }
            }

            openCount++;
            debug(methodName, "Opened");

            return zipFile;
        }
    }