- package com.study.zip;
- 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.util.zip.CRC32;
- import java.util.zip.CheckedOutputStream;
- import java.util.zip.Deflater;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipInputStream;
- import org.apache.tools.zip.ZipOutputStream;
- /**
- * 压缩与解压缩文件,目录工具类练习使用
- * 注意: 在java中,不管是目录还是文件实体,java都是把它当做一个ZipEntry对待的,ZipEntry的名字是相对于源头路径的全称
- * @author 乔宇鹏
- *
- */
- public class ZipUtil {
- /**
- * 将给定的文件压缩的指定的目录中,并指定压缩后文件的名称
- * @param src 源文件
- * @param targetDir 目标目录
- * @param zipFileName 给定压缩文件名称
- * @throws IOException
- */
- public static void copress(File src,File targetDir,String zipFileName) throws IOException{
- if( src.isDirectory())
- compress(src.listFiles(),targetDir,zipFileName);
- else
- compress(new File[]{src},targetDir,zipFileName);
- }
- /**
- * 将多个文件压缩到给定的目录中,并指定压缩文件名称
- */
- public static void compress(File[] files,File targetDir,String zipFileName ) throws IOException{
- File zipFile = createZip(files,targetDir,zipFileName);
- CheckedOutputStream cos = new CheckedOutputStream(
- new BufferedOutputStream( new FileOutputStream(zipFile)), new CRC32());
- ZipOutputStream zipWriter = new ZipOutputStream(cos);
- zipWriter.setEncoding("GBK");
- zipWriter.setLevel(Deflater.BEST_COMPRESSION);
- zipWriter.setMethod(ZipOutputStream.DEFLATED);
- String parentPath = "";
- for( File file : files ){
- if( file.isDirectory() )
- compressDir(file,zipWriter,parentPath);
- else
- compressFile(file,zipWriter,parentPath);
- }
- zipWriter.flush();
- zipWriter.close();
- }
- /** 进行压缩前的检查 **/
- private static File createZip(File[] files,File targetDir,String zipFileName) {
- if( files == null ) throw new RuntimeException("请选择被压缩的文件");
- if( zipFileName == null || "".equals(zipFileName)) zipFileName = "New ZipFile.zip";
- if( targetDir == null ) targetDir = files[0].getParentFile();
- if( targetDir.isFile() ) throw new RuntimeException("请选择正确的目录");
- File zipFile = new File(targetDir,zipFileName);
- if( zipFile.exists() )
- zipFile.delete();
- return zipFile;
- }
- /** 压缩一个文件 */
- private static void compressFile(File srcFile, ZipOutputStream zipWriter, String parentPath) throws IOException {
- StringBuilder sb = new StringBuilder();
- sb.append(parentPath)
- .append(srcFile.getName());
- String path = sb.toString();
- org.apache.tools.zip.ZipEntry entry = entry = new org.apache.tools.zip.ZipEntry(path);
- entry.setTime(srcFile.lastModified());
- entry.setComment(srcFile.getName());
- entry.setCompressedSize(srcFile.length()/2);
- entry.setMethod(ZipEntry.DEFLATED);
- zipWriter.putNextEntry(entry);
- BufferedInputStream reader = new BufferedInputStream(
- new FileInputStream(srcFile));
- byte[] cash = new byte[1024];
- int readed = 0;
- while( (readed = reader.read(cash)) > -1){
- zipWriter.write(cash,0,readed);
- }
- zipWriter.flush();
- zipWriter.closeEntry();
- }
- /** 压缩一个目录 **/
- private static void compressDir(File srcDir, ZipOutputStream zipWriter,String parentPath) throws IOException {
- StringBuilder sb = new StringBuilder();
- sb.append(parentPath)
- .append(srcDir.getName())
- .append("/");
- String path = sb.toString();
- org.apache.tools.zip.ZipEntry entry = new org.apache.tools.zip.ZipEntry(path);
- entry.setTime(srcDir.lastModified());
- entry.setComment(srcDir.getName());
- zipWriter.putNextEntry(entry);
- File[] files = srcDir.listFiles();
- for( File file : files ){
- if( file.isDirectory())
- compressDir(file, zipWriter, path);
- else
- compressFile(file, zipWriter, path);
- }
- zipWriter.closeEntry();
- }
- /** 完成解压缩任务 的方法 **/
- public static void uncompress(ZipInputStream zipReader ,File targetDir) throws IOException{
- ZipEntry zipEntry = null;
- while ((zipEntry = zipReader.getNextEntry()) != null) {
- if (zipEntry.isDirectory()) {
- File tempDir = new File(targetDir, zipEntry.getName());
- tempDir.mkdirs();
- zipReader.closeEntry();
- uncompress(zipReader, targetDir);
- } else {
- BufferedOutputStream zipWriter = new BufferedOutputStream(new FileOutputStream(
- new File(targetDir,zipEntry.getName())));
- byte[] cash = new byte[2048];
- int readed = 0;
- while ((readed = zipReader.read(cash)) > 0) {
- zipWriter.write(cash, 0, readed);
- }
- zipWriter.flush();
- zipWriter.close();
- }
- }
- }
- }
用java流 压缩文件与解压缩文件代码
最新推荐文章于 2024-01-22 00:35:20 发布