我想将 tar.gz 归档文件的内容保存在数据库表中。
归档文件包含 CSV 格式的txt文件。
这个想法是在数据库中为txt文件中的每一行插入一个新行。
问题是我无法分别读取文件的内容,然后继续下一个文件。
EntryTable 和 EntryTableLine 下面是Hibernate实体。
EntryTable 与 EntryTableLine (文件-EntryTable-可以有很多行-EntryTableLine-)处于 OneToMany 关系。
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()为null。
假设我的存档中有4个文件。每个文件内部有3行。但是,在数据库中,某些条目只有5行,而其他条目则没有。
谢谢!
答案 0 :(得分:0)
您可以使用Apache Commons Compress
中的TarArchiveInputStream
,如下所示(Reference):
TarArchiveInputStream input = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream("C:\\Users\\User\\Desktop\\Books\\test\\CoverLetter-Version2.gz")));
TarArchiveEntry entry = input.getNextTarEntry();
System.out.println(entry.getName()); // prints the name of file inside the tar
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
while (entry != null) {
br = new BufferedReader(new InputStreamReader(input)); // Read directly from tarInput
System.out.println("For File = " + currentEntry.getName());
String line;
while ((line = br.readLine()) != null) {
System.out.println("line="+line);
}
entry = input.getNextTarEntry();
}
答案 1 :(得分:0)
尝试直接从inputstream读取:
BufferedReader br = null;
while(tarEntry != null){
br = new BufferedReader(new InputStreamReader(tarEntry));