zoukankan      html  css  js  c++  java
  • java代码解压tar.bz2包

    package com.lxd.IO;
    
    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 org.apache.commons.compress.archivers.tar.TarArchiveEntry;
    import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
    import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
    import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
    import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            decompressTarBz2(new File("E:\lxd\lxd_LXD_001.tar.bz2"), "E:\lxd\nb");
        }
        public static void decompressTarBz2(File file, String outputDir) throws IOException {
            try (TarArchiveInputStream tarIn =
                         new TarArchiveInputStream(
                                 new BZip2CompressorInputStream(
                                         new FileInputStream(file)))) {
                createDirectory(outputDir, null);
                TarArchiveEntry entry;
                while ((entry = tarIn.getNextTarEntry()) != null) {
                    if (entry.isDirectory()) {
                        createDirectory(outputDir, entry.getName());
                    } else {
                        try (OutputStream out = new FileOutputStream(
                                new File(outputDir + File.separator + entry.getName()))) {
                            writeFile(tarIn, out);
                        }
                    }
                }
            }
        }
          /**
             * 写文件
             *
             * @param in
             * @param out
             * @throws IOException
             */
            public static void writeFile(InputStream in, OutputStream out) throws IOException {
                int length;
                byte[] b = new byte[1024*100];
                while ((length = in.read(b)) != -1) {
                    out.write(b, 0, length);
                }
            }
            /**
             * 创建目录
             *
             * @param outputDir
             * @param subDir
             */
            public static void createDirectory(String outputDir, String subDir) {
                File file = new File(outputDir);
                //子目录不为空
                if (!(subDir == null || subDir.trim().equals(""))) {
                    file = new File(outputDir + File.separator + subDir);
                }
                if (!file.exists()) {
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    file.mkdirs();
                }
            }
    }
  • 相关阅读:
    C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
    typedef void (*Fun) (void) 的理解——函数指针——typedef函数指针
    Source Insight 常用设置和快捷键大全
    关闭SourceInsight的大括号自动缩进
    MDK中One ELF Section per Function选项功能探究【转载】
    Application.DoEvents()的作用
    C#中Invoke的用法
    C# 委托的应用1:将方法作为参数传递给另一个方法[转]
    C#之委托(函数参数传递)【转】
    sk-learn 选择正确的估算器
  • 原文地址:https://www.cnblogs.com/xiandong/p/9858627.html
Copyright © 2011-2022 走看看