java 直接读取zip文件和文件内容 - 并发读取

java 直接读取zip文件和文件内容 - 并发读取

package com.lookcoder.utils;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class ReadFile implements Runnable {
    public static void main(String[] args) throws IOException {
        for (int i = 0; i < 500; i++) {
            new Thread(new ReadFile()).start();
        }
    }

    @Override
    public void run() {
        String path = "C:\\Users\\thunisoft\\Desktop\\plugman.zip";
        ZipInputStream zin = null;
        try {
            zin = new ZipInputStream(new FileInputStream(path), StandardCharsets.UTF_8);
            ZipFile zf = new ZipFile(path);
            ZipEntry ze;
            while ((ze = zin.getNextEntry()) != null) {
                if (ze.toString().endsWith("/node_modules/define-property/package.json")) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(zf.getInputStream(ze)));
                    String line;
                    StringBuilder sb = new StringBuilder();
                    while ((line = br.readLine()) != null) {
                        sb.append(line.toString().trim());
                    }
                    System.out.println(Thread.currentThread().getName() + " :: " + ze.getName() + " :: " + sb.toString());
                    br.close();
                    break;
                }
            }
            System.out.println();
            System.out.println();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zin != null) {
                try {
                    zin.closeEntry();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  

Thread-123 :: plugman/node_modules/base/node_modules/define-property/package.json :: {"_from": "define-property@^1.0.0","_id": "define-property@1.0.0","_inBundle": false,
Thread-214 :: plugman/node_modules/base/node_modules/define-property/package.json :: {"_from": "define-property@^1.0.0","_id": "define-property@1.0.0","_inBundle": false,
Thread-109 :: plugman/node_modules/base/node_modules/define-property/package.json :: {"_from": "define-property@^1.0.0","_id": "define-property@1.0.0","_inBundle": false,

  

posted @ 2020-05-08 16:19  GordonDicaprio  阅读(3837)  评论(0编辑  收藏  举报