JAVA Zip 압축 Tar 압축 tar. gz 압축

8519 단어
직접 코드 올 리 기:
        
            org.apache.commons
            commons-compress
            1.4.1
        


import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.log4j.Logger;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author Rorschach
 * @title: TestAll
 * @description: TODO
 * @date 2019-05-229:40
 */
public class CompressUtil {
    private String zipFileName;      //    Zip  
    private String sourceFileName;   //   (          )
    private static Logger logger = Logger.getLogger(CompressUtil.class.getName());

    public CompressUtil() {
    }

    public CompressUtil(String zipFileName, String sourceFileName) {
        this.zipFileName = zipFileName;
        this.sourceFileName = sourceFileName;
    }

    /**
     * @description:      ,            
     * @author Rorschach
     * @date 2019-05-23 15:55
     */
    public void fullTar(String tempDir) throws Exception {
        String path = getPath(tempDir);
        File allFile = new File(path);
        File[] files = allFile.listFiles();
        String resultName = "";
        for (File file : files) {
            resultName = path + "\\" + file.getName() + ".tar";
            new CompressUtil(resultName, path + "\\" + file.getName() + "\\").tar();
            gzipCompression(resultName, resultName + ".gz");
        }
    }

    /**
     * @description:               
     * @author Rorschach
     * @date 2019-05-23 15:54
     */
    public String getPath(String path) {
        File allFile = new File(path);
        File[] files = allFile.listFiles();
        String dirPath = "";
        for (int i = 0; i < files.length; i++) {
            logger.info(files[i].getName());
            if (files[i].isDirectory() && files[i].getName().startsWith("RADA_ST_DOR_L2_CAP")) {
                logger.info("true");
                dirPath = files[i].getParent();
                break;
            } else {
                String rs = getPath(files[i].getPath());
                if (rs != null && !"".equals(rs)) {
                    return rs;
                }
            }
        }
        if (dirPath.length() != 0) {
            return dirPath;

        } else {
            return "      ";
        }

    }

    /**
     * @description: zip  
     * @author Rorschach
     * @date 2019-05-23 15:54
     */
    public void zip() throws Exception {
        //File zipFile = new File(zipFileName);
        logger.info("   ...");
        ZipOutputStream out = null;
        BufferedOutputStream bos = null;
        try {
            //  zip   
            out = new ZipOutputStream(new FileOutputStream(zipFileName));
            File sourceFile = new File(sourceFileName);
            //    
            compress(out, sourceFile, sourceFile.getName());

        } catch (Exception e1) {
            logger.info("    !!!!!!!!!        ");
            e1.printStackTrace();
        } finally {
            if (bos != null)
                bos.close();
            if (out != null)
                out.close();
        }

        System.out.println("    ");

    }

    /**
     * @description: tar  
     * @author Rorschach
     * @date 2019-05-23 15:53
     */
    public void tar() throws Exception {
        //File zipFile = new File(zipFileName);
        logger.info("tar   ...");
        TarArchiveOutputStream out = null;
        BufferedOutputStream bos = null;
        try {
            //  tar   
            out = new TarArchiveOutputStream(new FileOutputStream(zipFileName), 512000);
            File sourceFile = new File(sourceFileName);
            //    
            tarCompress(out, sourceFile, sourceFile.getName());

        } catch (Exception e1) {
            logger.info("    !!!!!!!!!        ");
            e1.printStackTrace();
        } finally {

            if (out != null)
                out.close();
            if (bos != null)
                bos.close();
        }

        System.out.println("    ");

    }

    /**
     * @description: zip  
     * @author Rorschach
     * @date 2019-05-23 15:53
     */
    public void compress(ZipOutputStream out, File sourceFile, String base) throws Exception {
        //       (   )
        if (sourceFile.isDirectory()) {
            //         (     )
            File[] flist = sourceFile.listFiles();
            if (flist.length == 0)//       ,       zip            
            {
                System.out.println(base + "/");
                out.putNextEntry(new ZipEntry(base + "/"));
            } else//        ,     compress,          (    )    
            {
                for (int i = 0; i < flist.length; i++) {
                    compress(out, flist[i], base + "/" + flist[i].getName());
                }
            }
        } else//      (   ),    ,         ,       tar   
        {
            out.putNextEntry(new ZipEntry(base));
            FileInputStream fos = new FileInputStream(sourceFile);
            BufferedInputStream bis = new BufferedInputStream(fos);
            int tag;
            System.out.println(base);
            //       zip   ,    zip  ,           7Z  
            while ((tag = bis.read()) != -1) {
                out.write(tag);
            }
            fos.close();
            out.flush();
            bis.close();
        }
    }

    /**
     * @description:   Tar
     * @author Rorschach
     * @date 2019-05-23 15:52
     */
    public void tarCompress(TarArchiveOutputStream out, File sourceFile, String base) throws Exception {
        InputStream inputStream;
        //       (   )
        if (sourceFile.isDirectory()) {
            //         (     )
            File[] flist = sourceFile.listFiles();
            if (flist.length == 0)//       ,       zip            
            {
                System.out.println(base + "/");
                out.putArchiveEntry(new TarArchiveEntry(base + "/"));
            } else//        ,     compress,          (    )    
            {
                for (int i = 0; i < flist.length; i++) {
                    tarCompress(out, flist[i], base + "/" + flist[i].getName());
                }
            }
        } else//      (   ),    ,         ,       zip   
        {
            out.putArchiveEntry(new TarArchiveEntry(sourceFile, sourceFile.getName()));//           ,    
            inputStream = new FileInputStream(sourceFile);
            IOUtils.copy(inputStream, out);
            out.closeArchiveEntry();
        }
    }


    /**
     * @description:  tar   gz  
     * @author Rorschach
     * @date 2019-05-23 15:52
     */
    public static boolean gzipCompression(String filePath, String resultFilePath) throws IOException {
        logger.info(" gzipCompression -> Compression start!");
        InputStream fin = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        GzipCompressorOutputStream gcos = null;
        try {
            fin = Files.newInputStream(Paths.get(filePath));
            bis = new BufferedInputStream(fin);
            fos = new FileOutputStream(resultFilePath);
            bos = new BufferedOutputStream(fos);
            gcos = new GzipCompressorOutputStream(bos);
            byte[] buffer = new byte[1024];
            int read = -1;
            while ((read = bis.read(buffer)) != -1) {
                gcos.write(buffer, 0, read);
            }
        } finally {
            new File(filePath).delete();
            if (gcos != null)
                gcos.close();
            if (bos != null)
                bos.close();
            if (fos != null)
                fos.close();
            if (bis != null)
                bis.close();
            if (fin != null)
                fin.close();
        }
        logger.info(" gzipCompression -> Compression end!");
        return true;
    }


}

좋은 웹페이지 즐겨찾기