compress a source file/directory to a zip file

@param sources
@param target
@param filter
@throws IOException
768670
public static void compressZip(Resource[] sources, Resource target, ResourceFilter filter) throws IOException {
	ZipOutputStream zos = null;
	try {
	    zos = new ZipOutputStream(IOUtil.toBufferedOutputStream(target.getOutputStream()));
	    compressZip("", sources, zos, filter);
	}
	finally {
	    IOUtil.closeEL(zos);
	}
    }
1352738
public static void unzip(Path zip, Path target) throws IOException {
        try (final ZipFile zipFile = new ZipFile(zip.toFile())){
            unzip(zipFile, target);
        }
    }
642608
public static void copyFolder(String sourcePath, String targetPath, final String filterName,
         final boolean recursive) throws IOException
   {
      File path = new File(targetPath);
      if (!path.exists())
      {
         if (!path.mkdirs())
            throw new IOException("outdir can't be created");
      }
      File a = new File(sourcePath);
      String[] fileNames = a.list(new FilenameFilter()
      {
         public boolean accept(File dir, String fname)
         {
            if (new File(dir, fname).isDirectory())
            {
               return true;
            }
            else
            {
               return fname.endsWith(filterName);
            }
         }

      });
      File jarFile = null;
      if (fileNames != null)
      {
         for (String fileName : fileNames)
         {
            jarFile = new File(sourcePath + File.separator + fileName);

            if (jarFile.isFile())
            {
               copyFile(jarFile, targetPath);
            }
            if (recursive && jarFile.isDirectory())
            {
               copyFolder(sourcePath + File.separator + fileName, targetPath + File.separator + fileName, filterName,
                     recursive);
            }
         }
      }
   }
1004666
public static void zip(File[] sources, File target, String compressFileName) throws IOException {
		$.notEmpty(sources);
		$.notNull(target);
		if (!target.isDirectory()) {
			throw new IllegalArgumentException("The target can be a directory");
		}
		// 压缩后文件的名称
		compressFileName = $.notEmpty(compressFileName) ? compressFileName : sources[0].getName() + ".zip";
		FileOutputStream fileOut = new FileOutputStream(target.getAbsolutePath() + File.separator + compressFileName);
		ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(fileOut);
		for (int i = 0; i < sources.length; i++) {
			compress(sources[i], zipOut, null);
		}
		zipOut.close();
	}
26372
public static void compress(InputStream is, OutputStream os) throws Exception {
		GZIPOutputStream gos = new GZIPOutputStream(os);
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = is.read(data, 0, BUFFER)) != -1) {
			gos.write(data, 0, count);
		}
		gos.finish();
		gos.flush();
		gos.close();
	}
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();
			}
		}
	}
1045846
public static void extract(int format, Resource[] sources, Resource target) throws IOException {
	if (format == FORMAT_ZIP || format == FORMAT_TAR) {
	    for (int i = 0; i < sources.length; i++) {
		extract(format, sources[i], target);
	    }
	}
	else throw new IOException("can't extract in given format");
    }
1003056
public static void unZipFiles(File zipFile, File outputFolder) throws IOException {
        InputStream is = new BufferedInputStream(new FileInputStream(zipFile));
        try {
            unZipFiles(is, outputFolder);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
47089
public void unzip(final FilePath target) throws IOException, InterruptedException {
        // TODO: post release, re-unite two branches by introducing FileStreamCallable that resolves InputStream
        if (this.channel!=target.channel) {// local -> remote or remote->local
            final RemoteInputStream in = new RemoteInputStream(read(), Flag.GREEDY);
            target.act(new UnzipRemote(in));
        } else {// local -> local or remote->remote
            target.act(new UnzipLocal());
        }
    }
1362163
public static void extract(URL sourceURL, File targetFolder)
        throws IOException {
        if (sourceURL.getProtocol().equals("file")) {
            if (sourceURL.getFile().indexOf(".zip") > 0) {
                extractZipDistribution(sourceURL, targetFolder);
            }
            else if (sourceURL.getFile().indexOf(".tar.gz") > 0) {
                extractTarGzDistribution(sourceURL, targetFolder);
            }
            else {
                throw new IllegalStateException(
                    "Unknow packaging of distribution; only zip or tar.gz could be handled.");
            }
            return;
        }
        if (sourceURL.toExternalForm().indexOf("/zip") > 0) {
            extractZipDistribution(sourceURL, targetFolder);
        }
        else if (sourceURL.toExternalForm().indexOf("/tar.gz") > 0) {
            extractTarGzDistribution(sourceURL, targetFolder);
        }
        else {
            throw new IllegalStateException(
                "Unknow packaging; only zip or tar.gz could be handled. URL was " + sourceURL);
        }
    }
834749
public function uncompress($source, $target)
    {
        if ($this->zip->open($source, \ZipArchive::CHECKCONS) === false) {
            return false;
        }

        if ($this->zip->extractTo(dirname($target), basename($target)) === false) {
            $this->zip->close();

            return false;
        }

        return $this->zip->close();
    }
479467
public function compress($source, $target = '')
    {
        if ($target && $target != $source) {
            return copy($source, $target);
        }

        return true;
    }
1040524
public static void copyDirectory(File source, File target) {
        if (source.isDirectory()) {
            if (!target.exists()) {
                target.mkdir();
            }
            for (String child : source.list()) {
                copyDirectory(new File(source, child), new File(target, child));
            }
        } else {
            try {
                write(new FileInputStream(source), new FileOutputStream(target));
            } catch (IOException e) {
                if (target.isDirectory()) {
                    if (!target.exists()) {
                        if (!target.mkdirs()) {
                            throw E.ioException("cannot copy [%s] to [%s]", source, target);
                        }
                    }
                    target = new File(target, source.getName());
                } else {
                    File targetFolder = target.getParentFile();
                    if (!targetFolder.exists()) {
                        if (!targetFolder.mkdirs()) {
                            throw E.ioException("cannot copy [%s] to [%s]", source, target);
                        }
                    }
                }
                try {
                    write(new FileInputStream(source), new FileOutputStream(target));
                } catch (IOException e0) {
                    throw E.ioException(e0);
                }
            }
        }
    }
644385
public static void extract(int format, Resource source, Resource target) throws IOException {
	if (format == FORMAT_ZIP) extractZip(source, target);
	else if (format == FORMAT_TAR) extractTar(source, target);
	else if (format == FORMAT_GZIP) extractGZip(source, target);
	else if (format == FORMAT_TGZ) extractTGZ(source, target);
	else throw new IOException("can't extract in given format");
    }
738441
public static void unZipFiles(InputStream inputStream, File outputFolder) throws IOException {
        ZipInputStream zis = new ZipInputStream(inputStream);
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            File file = new File(outputFolder, ze.getName());
            OutputStream os = new BufferedOutputStream(FileUtils.openOutputStream(file));

            try {
                IOUtils.copy(zis, os);
            } finally {
                IOUtils.closeQuietly(os);
            }

            zis.closeEntry();
            ze = zis.getNextEntry();
        }
    }
348437
public static void copy(File source, File target) throws IOException {
        if (!source.exists()) {
            throw new FileNotFoundException("Source file not found:" + source.getAbsolutePath());
        }

        if (!target.exists() && !target.getParentFile().exists() && !target.getParentFile().mkdirs()) {
            throw new IOException("Can't create target directory:" + target.getParentFile().getAbsolutePath());
        }
        if (source.isDirectory()) {
            target.mkdirs();
            File[] files = source.listFiles();
            if (files != null) {
                for (File child : files) {
                    copy(child, new File(target, child.getName()));
                }
            }
        } else {
            FileInputStream is = new FileInputStream(source);
            FileOutputStream os = new FileOutputStream(target);
            io.fabric8.utils.Files.copy(is, os);
        }
    }
127951
public void processZips(File[] zipFiles, File destDir) throws IOException {
        for (int i = 0; i < zipFiles.length; i++) {
            processFile(zipFiles[i], destDir);
        }
    }