1.添加maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.14</version> </dependency>
2.开始测试
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.io.IOUtils; public class ZipConpress { static int bufferSize = 8192;//单位bytes /** * 用于单文件压缩 */ public static void doCompress(File srcFile, File destFile) throws IOException { ZipArchiveOutputStream out = null; InputStream is = null; try { is = new BufferedInputStream(new FileInputStream(srcFile), bufferSize); out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferSize)); ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName()); entry.setSize(srcFile.length()); out.putArchiveEntry(entry); IOUtils.copy(is, out); out.closeArchiveEntry(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } /** * 用于多文件压缩 */ public static void doCompressFiles(List<File> srcFiles, File destFile) throws IOException { ZipArchiveOutputStream out = null; InputStream is = null; try { out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferSize)); for(File srcFile:srcFiles){ is = new BufferedInputStream(new FileInputStream(srcFile), bufferSize); ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName()); entry.setSize(srcFile.length()); out.putArchiveEntry(entry); IOUtils.copy(is, out); } out.closeArchiveEntry(); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(out); } } public static void doDecompress(File srcFile, File destDir) throws IOException { ZipArchiveInputStream is = null; try { is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferSize)); ZipArchiveEntry entry = null; while ((entry = is.getNextZipEntry()) != null) { if (entry.isDirectory()) { File directory = new File(destDir, entry.getName()); directory.mkdirs(); } else { OutputStream os = null; try { os = new BufferedOutputStream( new FileOutputStream(new File(destDir, entry.getName())), bufferSize); IOUtils.copy(is, os); } finally { IOUtils.closeQuietly(os); } } } } finally { IOUtils.closeQuietly(is); } } public static void main(String[] args) { try { //ZipConpress.doCompress(new File("/pathto/abc.txt"),new File("/pathto/abc-test.zip")); List<File> files=new ArrayList<File>(); files.add(new File("/pathto/abc.txt")); files.add(new File("/pathto/xyz.txt")); ZipConpress.doCompressFiles(files,new File("/pathto/abc-test.zip")); // File doDecompressDir = new File("/pathto/abc-test1723"); // if (!doDecompressDir.exists()) { // doDecompressDir.mkdirs(); // } // ZipConpress.doDecompress(new File("/pathto/abc-test.zip"), doDecompressDir // ); } catch (IOException e) { e.printStackTrace(); } } }