zoukankan      html  css  js  c++  java
  • 廖雪峰Java6 IO编程-2input和output-5操作zip

    1.ZipInputStream是一种FilterInputStream

    • 可以直接读取zip的内容
    • InputStream->FilterInputStream->InflateInputStream->ZipInputStream->JarInputStream
    • JarInputStream使用ZipInputStream派生出来的,可以直接读取jar文件的MANIFEST.MF文件

    ZipInputStream的基本用法:

    try(ZipInputStream zip = new ZipInputStream()...){
        ZipEntry entry = null;
        while((entry=zip.getNextEntry())!=null){//getNextEntry返回null,zip流结束
            String name = entry.getName();//每一个entry都表示一个压缩文件或者目录
            if(!entry.isDirectory()){
                int n;
                while((n=zip.read()) != -1){...}//如果entry是压缩文件,就不断读取,直到返回-1
            }
        }
    }
    

    问题:为什么read()传入的zip,不是entry?zip流是按照单个文件读取的。Entry代表当前文件或目录,zip.read()读取返回-1,当前entry结束,开始下一个entry。

    2.ZipOutputStream是一种FilterOutputStream

    • 可以直接写入Zip的内容
    try(ZipOutputStream zip = new ZipOutputStream(...)){
        File[] files = ...
        for(File file:files){
            zip.putNextEntry(new ZipEntry(file.getName()));//未考虑文件目录的层次结构;如果考虑层次结构,传入的name需要使用相对路径
            zip.write(getFilterDataAsBytes(file));//写入byte数据
            zip.closeEntry();//结束这个文件的打包
        }
    }
    ##    3.示例
    ```#java
    public class Main {
    
        public static void main(String[] args) throws IOException {
            try(//需要调用ZipInputStream的getNextEntry(),因此没有将zip转型为InputStream
                ZipInputStream zip = new ZipInputStream(
                        new BufferedInputStream(
                                new FileInputStream("./src/main/java/com/testList/agent.jar")))){
                ZipEntry entry = null;
                while((entry=zip.getNextEntry())!=null){
                    if(entry.isDirectory()){
                        System.out.println("D "+entry.getName()+"	"+entry.getSize());
    
                    }else{
                        System.out.println("F "+entry.getName()+"	"+entry.getSize());//打印entry的大小
                        printFileContent(zip);
                    }
                }
            }
        }
        static void printFileContent(ZipInputStream zip) throws IOException{
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int n = 0;
            while((n=zip.read(buffer))!=-1){
                output.write(buffer,0,n);
            }
            byte[] data = output.toByteArray();
            System.out.println("    size:"+data.length);
        }
    }
    

    4.总结:

    • ZipInputStream可以读取Zip格式的流
    • ZipOutputStream可以把数据写入Zip
    • ZipInputStream/ZipOutputStream都是FilterInputStream/FilterOutputStream
    • 配合FileInputStream和File OutputStream就可以读写Zip文件
  • 相关阅读:
    使用helm管理复杂kubernetes应用
    helm repository 相关
    PSQLException: An I/O error occurred while sending to the backend.
    使用helm进行kubernetes包管理
    Slave作为其它Slave的Master时使用
    ext3是对ext2文件系统的一个扩展高性能日志文件系统
    ready是先执行的,load后执行,DOM文档的加载步骤
    jQuery上定义插件并重设插件构造函数
    在PHP与HTML混合输入的页面或者模板中就需要对PHP代码进行闭合
    decode 函数将字符串从某种编码转为 unicode 字符
  • 原文地址:https://www.cnblogs.com/csj2018/p/10660080.html
Copyright © 2011-2022 走看看