java 压缩/解压【tar.gz】

环境

操作系统:win7
java:jdk7
第三方包:commons-compress-1.14.jar

需求

不管是文件夹还是常规文件,实现基本的打包压缩。

思路:
①先把需要压缩的文件,打包成.tar文件。
②使用gzip把刚刚打包的.tar文件进行压缩(tar.gz)

这里写图片描述


打包代码

http://commons.apache.org/proper/commons-compress/examples.html

官网:

TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
tarOutput.putArchiveEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();

官网给的代码很简单,也就是一个思路。

我的完整代码:

/**
     * 归档
     * @param entry
     * @throws IOException
     * @author yutao
     * @return 
     * @date 2017年5月27日下午1:48:23
     */
    private static String archive(String entry) throws IOException {
        File file = new File(entry);

        TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(file.getAbsolutePath() + ".tar"));
        String base = file.getName();
        if(file.isDirectory()){
            archiveDir(file, tos, base);
        }else{
            archiveHandle(tos, file, base);
        }

        tos.close();
        return file.getAbsolutePath() + ".tar";
    }

/**
     * 递归处理,准备好路径
     * @param file
     * @param tos
     * @param base 
     * @throws IOException
     * @author yutao
     * @date 2017年5月27日下午1:48:40
     */
    private static void archiveDir(File file, TarArchiveOutputStream tos, String basePath) throws IOException {
        File[] listFiles = file.listFiles();
        for(File fi : listFiles){
            if(fi.isDirectory()){
                archiveDir(fi, tos, basePath + File.separator + fi.getName());
            }else{
                archiveHandle(tos, fi, basePath);
            }
        }
    }

/**
     * 具体归档处理(文件)
     * @param tos
     * @param fi
     * @param base
     * @throws IOException
     * @author yutao
     * @date 2017年5月27日下午1:48:56
     */
    private static void archiveHandle(TarArchiveOutputStream tos, File fi, String basePath) throws IOException {
        TarArchiveEntry tEntry = new TarArchiveEntry(basePath + File.separator + fi.getName());
        tEntry.setSize(fi.length());

        tos.putArchiveEntry(tEntry);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fi));

        byte[] buffer = new byte[1024];
        int read = -1;
        while((read = bis.read(buffer)) != -1){
            tos.write(buffer, 0 , read);
        }
        bis.close();
        tos.closeArchiveEntry();//这里必须写,否则会失败
    }

上面代码使用方法:只需要调archive(文件或目录路径)

archive方法里面主要调了两个方法archiveDirarchiveHandle,返回的是最后打包文件名的全路径。
archiveDir方法是个递归的方法。目的就是为了把文件夹中里的所有文件都遍历出来。其实更确切的说是把文件相对路径(相对于打包目录,我这里就是test目录)通过递归拼接好,为真正的打包写入操作做准备。
archiveHandle方法,是真正的打包方法。参数为:1、打包输出流2、需要打包的文件3、打包文件里的路径(之前拼接好的路径)。
tos.closeArchiveEntry()这里一定要注意;官网api解释为:
http://commons.apache.org/proper/commons-compress/javadocs/api-release/index.html

所有包含数据的entry都必须调用此方法。
原因是为了满足缓存区基于记录的写入,我们必须缓冲写入流里的数据。因此可能有数据片段仍然被组装(我认为应该是肯能还有数据在缓存区里),所以必须在该条目(entry)关闭之前写入输出流,并写入下个条目。

压缩

这里使用的是gzip进行压缩,该压缩最适合单一文件的压缩,这也是为什么要先打包成tar文件的原因。

我的压缩代码:

/**
     * 把tar包压缩成gz
     * @param path
     * @throws IOException
     * @author yutao
     * @return 
     * @date 2017年5月27日下午2:08:37
     */
    public static String compressArchive(String path) throws IOException{
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));

        GzipCompressorOutputStream gcos = new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(path + ".gz")));

        byte[] buffer = new byte[1024];
        int read = -1;
        while((read = bis.read(buffer)) != -1){
            gcos.write(buffer, 0, read);
        }
        gcos.close();
        bis.close();
        return path + ".gz";
    }

该方法很简单,没什么好说的。

使用方法:调用compressArchive(需要解压文件的路径)

解压

①先解压tar.gztar
②再对tar进行解压,并删除tar文件。

解压gz文件

官网代码:
http://commons.apache.org/proper/commons-compress/examples.html

InputStream fin = Files.newInputStream(Paths.get("archive.tar.gz"));
BufferedInputStream in = new BufferedInputStream(fin);
OutputStream out = Files.newOutputStream(Paths.get("archive.tar"));
GZipCompressorInputStream gzIn = new GZipCompressorInputStream(in);
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = gzIn.read(buffer))) {
    out.write(buffer, 0, n);
}
out.close();
gzIn.close();

我的代码:

/**
     * 解压
     * @param archive
     * @author yutao
     * @throws IOException 
     * @date 2017年5月27日下午4:03:29
     */
    private static void unCompressArchiveGz(String archive) throws IOException {

        File file = new File(archive);

        BufferedInputStream bis = 
        new BufferedInputStream(new FileInputStream(file));

        String fileName = 
        file.getName().substring(0, file.getName().lastIndexOf("."));

        String finalName = file.getParent() + File.separator + fileName;

        BufferedOutputStream bos = 
        new BufferedOutputStream(new FileOutputStream(finalName));

        GzipCompressorInputStream gcis = 
        new GzipCompressorInputStream(bis);

        byte[] buffer = new byte[1024];
        int read = -1;
        while((read = gcis.read(buffer)) != -1){
            bos.write(buffer, 0, read);
        }
        gcis.close();
        bos.close();

        unCompressTar(finalName);
    }

解压tar

官网代码:
http://commons.apache.org/proper/commons-compress/examples.html

TarArchiveEntry entry = tarInput.getNextTarEntry();
byte[] content = new byte[entry.getSize()];
LOOP UNTIL entry.getSize() HAS BEEN READ {
    tarInput.read(content, offset, content.length - offset);
}

我的代码:

/**
     * 解压tar
     * @param finalName
     * @author yutao
     * @throws IOException 
     * @date 2017年5月27日下午4:34:41
     */
    private static void unCompressTar(String finalName) throws IOException {

        File file = new File(finalName);
        String parentPath = file.getParent();
        TarArchiveInputStream tais = 
        new TarArchiveInputStream(new FileInputStream(file));

        TarArchiveEntry tarArchiveEntry = null;

        while((tarArchiveEntry = tais.getNextTarEntry()) != null){
            String name = tarArchiveEntry.getName();
            File tarFile = new File(parentPath, name);
            if(!tarFile.getParentFile().exists()){
                tarFile.getParentFile().mkdirs();
            }

            BufferedOutputStream bos = 
            new BufferedOutputStream(new FileOutputStream(tarFile));

            int read = -1;
            byte[] buffer = new byte[1024];
            while((read = tais.read(buffer)) != -1){
                bos.write(buffer, 0, read);
            }
            bos.close();
        }
        tais.close();
        file.delete();//删除tar文件
    }

如何使用上面的代码

public static void main(String[] args) throws IOException {

        String entry = "C:\\Users\\yutao\\Desktop\\pageage\\test";//需要压缩的文件夹
        String archive = archive(entry);//生成tar包
        String path = compressArchive(archive);//生成gz包

//      unCompressArchiveGz("C:\\Users\\yutao\\Desktop\\pageage\\test.tar.gz");//解压
    }

参考地址:
http://snowolf.iteye.com/blog/648652

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
在Android中.tar.gz文件,你可以按照以下步骤进行操作: 1. 首先,你需要在终端或命令提示符中使用以下命令来.tar.gz文件: ``` tar -zxvf your_file.tar.gz ``` 其中,your_file.tar.gz是你要的文件名。 2. 如果你想在Android项目中实现.tar.gz文件的压缩功能,你可以使用一些开源库来完成。比如,你可以使用Apache Commons Compress库来处理.tar.gz文件。首先,你需要在项目的build.gradle文件中添加以下依赖: ``` dependencies { //其他依赖 implementation 'org.apache.commons:commons-compress:1.18' } ``` 3. 接下来,你可以在代码中使用Apache Commons Compress库来.tar.gz文件。首先,你需要导入相关类: ```java import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; ``` 4. 然后,你可以使用以下代码来.tar.gz文件: ```java File inputFile = new File("your_file.tar.gz"); File outputDir = new File("output_directory"); try (TarArchiveInputStream tarIn = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(inputFile)))) { TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { if (entry.isDirectory()) { File directory = new File(outputDir, entry.getName()); directory.mkdirs(); } else { File outputFile = new File(outputDir, entry.getName()); try (OutputStream outputStream = new FileOutputStream(outputFile)) { IOUtils.copy(tarIn, outputStream); } } } } catch (IOException e) { e.printStackTrace(); } ``` 其中,your_file.tar.gz是你要的文件名,output_directory是你想要将后的文件保存的目录。 请注意,在使用Apache Commons Compress库之前,你需要在项目中添加相应的jar包。你可以从官方网站下载这些jar包,或者使用Maven或Gradle等构建工具来自动下载并管理这些依赖。 希望以上信息能帮助到你,在Android中成功.tar.gz文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山鬼谣me

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值