• Java解压和压缩Tar


    package org.jeecg.runner;
    
    import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
    import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
    import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
    import java.io.*;
    
    public class TarUtils {
        public static void main(String[] args) throws IOException {
            tarFile("G:/Demo/cfcs_plugin.ini","G:/Demo/测试.tar");
            unTarFile("G:/Demo/测试","G:/Demo/测试.tar");
        }
    
        /**
         *压缩tar包
         *
         * @param filePath  文件路径
         * @param tarPath   tar包生成路径
         * @throws IOException
         */
        public static void tarFile(String filePath,String tarPath) throws IOException {
            File file = new File(filePath);
            TarArchiveEntry tae = new TarArchiveEntry(file);
            tae.setSize(file.length());
            //不加tae.setName,压缩文件里是全路径(filePath有几层,压缩包里有几层)
            tae.setName(new String(file.getName().getBytes("GBK"),"ISO-8859-1"));
            TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(new File(tarPath)));
            taos.putArchiveEntry(tae);
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            int count;
            byte data[] = new byte[1024];
            while((count = bis.read(data,0,1024)) !=-1){
                taos.write(data,0,count);
            }
            bis.close();
            taos.closeArchiveEntry();
        }
    
        /**
         * 解压tar包
         *
         * @param unTarPath     解压缩tar包路径
         * @param tarPath       tar包路径
         * @throws IOException
         */
        public static void unTarFile(String unTarPath,String tarPath) throws IOException {
            TarArchiveEntry tae = null;
            TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(new File(tarPath)));
            while((tae = tais.getNextTarEntry()) != null){
                String dir = unTarPath + File.separator + tae.getName();
                File dirFile = new File(dir);
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dirFile));
                int count;
                byte data[] = new byte[1024];
                while((count = tais.read(data,0,1024)) !=-1){
                    bos.write(data,0,count);
                }
                bos.close();
            }
        }
    }
  • 相关阅读:
    心得体会,搞清楚你为什么学习C++?
    完整版本的推箱子小游戏,最简单的纯C语言打造
    联合体、枚举体初步了解及运用
    结构体的初步了解
    使用 Appium 测试微信小程序 Webview——打开调试功能
    Jmeter 使用ssh command 链接linux
    jmeter响应内容乱码问题
    Mac 更新 node版本
    解决jenkins + ant + jmeter发送邮件失败的问题:java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage
    bash特殊字符-2
  • 原文地址:https://www.cnblogs.com/raitorei/p/16325707.html
Copyright © 2020-2023  润新知