下载一个zip,提取CSV和解析它 - 所有的记忆 - Java的

问题描述:

我有一个包含.CSV文件的.zip文件的URL。下载一个zip,提取CSV和解析它 - 所有的记忆 - Java的

我用Java编写需要下载此.zip文件和访问的.zip里面的CSV文件,并在使用Apache下议院CSV CSVRecords的名单解析他们的应用程序。我不想将任何文件写入磁盘,因为这是性能浪费。

这是我迄今为止(我省略了所有的错误处理,现在,它只是一个POC):

URL url = new URL(myURLString); 
InputStream input = url.openStream(); 
ZipInputStream zipIn = new ZipInputStream(input); 
ZipEntry entry; 
while((entry = zipIn.getNextEntry()) != null) { 
    InputStreamReader isr = new InputStreamReader(zipIn); 
    CSVParser csv = new CSVParser(isr, CSVFormat.DEFAULT); 
    List<CSVRecord> records = csv.getRecords(); <----- THIS IS WHERE IT HANGS! 
} 

出于某种原因,我想不通为什么它挂起CSVParser尝试时阅读文件。任何帮助是极大的赞赏!

P.S:我可以读一个CSV就好了,当它不是在一个zip,因为这样的:

URL url = new URL(myURLString); 
InputStream input = url.openStream(); 
InputStreamReader reader= new InputStreamReader(input); 
CSVParser csv = new CSVParser(reader, CSVFormat.DEFAULT); 
List<CSVRecord> records = csv.getRecords(); 
+0

u能尝试CSVFormat.Excel,让我知道会发生什么? – user641887

+0

它只是挂在那儿,不返回任何东西 –

+0

可以保证的zip文件的内容是正确的,它与扩展的.csv的所有文件只 – user641887

也许尝试使用不同的解析器。使用univocity-parsers可能会报告处理您的文件的任何错误。

只是更改为:

URL url = new URL(myURLString); 
InputStream input = url.openStream(); 
ZipInputStream zipIn = new ZipInputStream(input); 
ZipEntry entry; 
//configure the parser to detect the CSV format automatically 
CsvParserSettings parserSettings = new CsvParserSettings(); 
parserSettings.detectFormatAutomatically(); 
//use this if the files are small (less than 50mb each) 
//parserSettings.setReadInputOnSeparateThread(false); 

CsvParser csv = new CsvParser(parserSettings); 

while((entry = zipIn.getNextEntry()) != null) { 
    InputStreamReader isr = new InputStreamReader(zipIn); 
    List<Record> records = csv.parseAllRecords(isr); 
} 

希望它能帮助。

免责声明:我这个libary的作者。它是开源和免费的(Apache 2.0许可)