commons-compress(apache压缩工具包)

Posted erdanyang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了commons-compress(apache压缩工具包)相关的知识,希望对你有一定的参考价值。

一、添加压缩文件:

package aaaaa.my.test.cmdoption;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.file.Paths;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String input = "D:\eclipseWorkspace\cmdoption\src";
        String output = "D:\eclipseWorkspace\cmdoption\target";
        input = new File(input).getPath();//路径标准化
        output = new File(output).getPath();//路径标准化
        LOGGER.info(input);
        LOGGER.info(output);
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(Paths.get(output, "erdan.csar").toFile());
            ArchiveOutputStream zos = new ArchiveStreamFactory().createArchiveOutputStream("zip", outputStream);
            zos.putArchiveEntry(new ZipArchiveEntry("TOSCA-Metadata/TOSCA.meta"));
            PrintWriter pw = new PrintWriter(zos);
            pw.println("TOSCA-Meta-Version: 1.0");
            pw.println("CSAR-Version: 1.0");
            String versionString = "Created-By: Winery " + "zte";
            pw.println(versionString);
            pw.println("Entry-Definitions: " + "Definitions/" + "serviceTemplateId" + "."
                + "yaml");
            pw.println();
            pw.println(
                "Name: " + "Definitions/" + "serviceTemplateId" + "." + "yaml");
            pw.println("Content-Type: " + "application/vnd.oasis.tosca.definitions");
            pw.println();
            pw.flush();
            pw.close();
            zos.closeArchiveEntry();
            zos.flush();
            zos.close();
        }catch(Exception e) {
            
        }finally {
            if(outputStream != null) {
                try {
                    outputStream.close();
                }catch(Exception e) {
                    
                }    
            }
        }    
    }
}

后面研究完java.io会进行详细讲解

以上是关于commons-compress(apache压缩工具包)的主要内容,如果未能解决你的问题,请参考以下文章

使用commons-compress压缩的zip,用winrar解压出错?

Apache Commons:简单的使用Compress创建7z压缩文件

Java解压tar.Z文件(使用Apache Commons-compress)

解压.7z格式压缩文件,Java

Java 压缩文件夹工具类(包含解压)

zip解压线程(解决中文问题)