java 打包压缩成Zip文件

java 打包压缩成Zip文件

package com.xxx.ietm.utils;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author zhanghui
 * @Title FileToZipUtils
 * @ProjectName ietm_csdb
 * @Description: 通过路径打zip
 * @date 2020/12/22 0022-下午 5:25
 */
public class FileToZipUtils {
     
    /**
     * @Description //压缩文件
     * @Param sourceFilewPaht  源文件路径
     * @Param zipFilePath  压缩后文件存储路径
     * @Param zipFileName  压缩文件名
     * @Author zhanghui
     * @Return
     * @Date 2020/12/22 0022-下午 5:28
    */
    public static void compressToZip(String sourceFilewPaht,String zipFilePath,String zipFileName){
     
        File sourceFile = new File(sourceFilewPaht);
        File zipPath = new File(zipFilePath);
        if(!zipPath.exists()){
     
            zipPath.mkdirs();
        }

        File zipFile = new File(zipPath+File.separator+zipFileName+".zip");
        try 
        (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))){
     
            writeZip(sourceFile,"",zos);
            //文件压缩完成后,删除被压缩文件
            boolean flag = deleteDir(sourceFile);
            log.info("删除被压缩文件[" + sourceFile + "]标志:{}", flag);
        } catch (Exception e) {
     
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(),e.getCause());
        }
    }

    /**
     * @Description // 遍历所有需要压缩的文件
     * @Param  file 源文件目录
     * @Param  parentPath 压缩文件目录
     * @Param  zos 文件流
     * @Author zhanghui
     * @Return
     * @Date 2020/12/22 0022-下午 5:39
    */
    public  static void writeZip(File file ,String parentPath,ZipOutputStream zos){
     
        if(file.isDirectory()){
     
            // 如果是目录
            parentPath += file.getName() + File.separator;
            File[] files = file.listFiles();
            for(File f:files){
     
                writeZip(f,parentPath,zos);
            }
        }else{
     
            //是文件
            try {
     
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
                zos.putNextEntry(zipEntry);
                int len ;
                byte[] buffer = new byte[1024 * 10 ];
                while ((len = bis.read(buffer,0,buffer.length)) != -1){
     
                    zos.write(buffer,0,len);
                    zos.finish();
                }
              
            } catch (Exception e) {
     
                e.printStackTrace();
                throw new RuntimeException(e.getMessage(),e.getCause());
            }
        }
    }
    /**
     * @Description // 删除文件夹
     * @Param  
     * @Author zhanghui
     * @Return
     * @Date 2020/12/22 0022-下午 5:39
    */
public static boolean deleteDir(File dir) {
     
        if (dir.isDirectory()) {
     
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
     
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
     
                    return false;
                }
            }
        }
        //删除空文件夹
        return dir.delete();
    }
/**
     * @Description //拷贝文件到指定目录
     * @Param    source  源文件地址
     * @Param    dest  目标地址
     * @Author zhanghui
     * @Return
     * @Date 2020/12/23 0023-下午 2:19
    */
    public static  void copyFileUsingFileChannels(File source,File dest) throws IOException{
     
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
     
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            outputChannel.transferFrom(inputChannel,0,inputChannel.size());
        } finally {
     
           inputChannel.close();
           outputChannel.close();
        }
    }

    public static void main(String[] args) {
     
    
  // deleteDir(new File("C:\\fsa\\1520\\xml"));  
        //compressToZip("C:\\fsaxml\\xml","C:\\fsa\\1520","fsa1520");

 File source = new File("C:\\Users\\Administrator\\Downloads\\20201223100501\\DDN-HJP500-HJBGB-SIM01-2020-000001.xml");
       File dest = new File("C:\\csdndir\\ddn\\20201223115754\\DDN-HJP500-HJBGB-SIM01-2020-000001.xml");
        try {
     
            copyFileUsingFileChannels(source,dest);
        } catch (IOException e) {
     
            e.printStackTrace();
        }
    }
}

最终效果:
java 打包压缩成Zip文件_第1张图片
解压后的文件:
java 打包压缩成Zip文件_第2张图片

你可能感兴趣的:(Java,打包压缩成Zip包,java,zip)