我想将tar.gz存档的内容保存在数据库表中。
存档包含CSV格式的 txt 文件。
这个想法是在数据库中为 txt 文件中的每一行插入一个新行。
问题是我无法单独读取文件的内容然后继续下一个文件。
EntryTable和EntryTableLine下面是 Hibernate 实体。
EntryTable与EntryTableLine是OneToMany关系(一个文件 -EntryTable- 可以有很多行 -EntryTableLine-)。
public static final int TAB = 9;
FileInputStream fileInputStream = new FileInputStream(fileLocation);
GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
TarArchiveInputStream tar = new TarArchiveInputStream(gzipInputStream);
BufferedReader reader = new BufferedReader(new InputStreamReader(tar));
// Columns are delimited with TAB
CSVFormat csvFormat = CSVFormat.TDF.withHeader().withDelimeter((char) TAB);
CSVParser parser = new CSVParser(reader, csvFormat);
TarArchiveEntry tarEntry = tar.getNextTarEntry();
while(tarEntry != null){
EntryTable entryTable = new EntryTable();
entryTable.setFilename(tarEntry.getName());
if(reader != null){
// Here is the problem
for(CSVRecord record : parser){
//this could have been a StringBuffer
String line;
int i = 1;
for(String val : record){
line = "<column" + i + ">" + val + "</column" + i + ">";
}
EntryTableLine entryTableLine = new EntryTableLine();
entryTableLine.setContent(line);
entryDao.saveLine(entryTableLine);
}
}
tarEntry = tar.getNextTarEntry();
}
我尝试将tarEntry.getFile()转换为InputStream,但不幸的是tarEntry.getFile()为空。
假设我在存档中有 4 个文件。每个文件里面有 3 行。但是,在数据库中,一些条目有 5 行,而另一些则没有。
谢谢 !