美文网首页
0001.使用CSVParser逐行读取ZIP压缩过的CSV文件

0001.使用CSVParser逐行读取ZIP压缩过的CSV文件

作者: 笑着字太黑 | 来源:发表于2022-01-06 10:59 被阅读0次
package t;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

public class CSVParserTest {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String filePath = "XXX.zip";
        BufferedReader br = null;
        try ( 
                ZipFile zf = new ZipFile(filePath);  
                InputStream in = new BufferedInputStream(new FileInputStream(filePath));  
                ZipInputStream zin = new ZipInputStream(in);  
            ) {
            ZipEntry ze = zin.getNextEntry();  
            br = new BufferedReader(  
                    new InputStreamReader(zf.getInputStream(ze)));  
//          String line = null;
//          while ((line = br.readLine()) != null) {
//              System.out.println(line);
//          }
              CSVParser parser = CSVFormat.EXCEL.withIgnoreEmptyLines(true).withFirstRecordAsHeader()
                  .withIgnoreSurroundingSpaces(true).parse(br);
              var iter = parser.iterator();
              int index = 0;
              System.out.println(index++);
              long startTime = System.currentTimeMillis();
              while (iter.hasNext()) {
                  System.out.println(index++);
                  CSVRecord csvRecord = iter.next();
//                Map<String, String> row = csvRecord.toMap();
//                System.out.println(row.toString());
              }
              long endTime = System.currentTimeMillis();
              System.out.println("time:"+(endTime-startTime));
        } finally {
            br.close();
        }
        System.out.println("complete");
    }
    
    public static void readZipFile(String file) throws Exception {  
        ZipFile zf = new ZipFile(file);  
        InputStream in = new BufferedInputStream(new FileInputStream(file));  
        ZipInputStream zin = new ZipInputStream(in);  
        ZipEntry ze;  
        while ((ze = zin.getNextEntry()) != null) {  
            if (ze.isDirectory()) {
            } else {  
                System.err.println("file - " + ze.getName() + " : "  
                        + ze.getSize() + " bytes");  
                long size = ze.getSize();  
                if (size > 0) {  
                    BufferedReader br = new BufferedReader(  
                            new InputStreamReader(zf.getInputStream(ze)));  
                    String line;  
                    while ((line = br.readLine()) != null) {  
                        System.out.println(line);  
                    }  
                    br.close();  
                }  
                System.out.println();  
            }
        }  
        zin.closeEntry();  
    }  

}

相关文章

网友评论

      本文标题:0001.使用CSVParser逐行读取ZIP压缩过的CSV文件

      本文链接:https://www.haomeiwen.com/subject/vcwlcrtx.html