Skip to content

Commit

Permalink
fix #2: extract dz file by using Gzip stream
Browse files Browse the repository at this point in the history
dictzip is completely compatible with the gzip file format. We want to
extract everything then import to database. Therefore "extra field"[1]
is not meaningful to us.

[1] dictzip use extra field of gzip to store information for Random
Access.
  • Loading branch information
walkingice committed Jun 10, 2017
1 parent e2935b7 commit ad70aec
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions app/src/main/java/org/zeroxlab/momodict/archive/TarExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.io.FileUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -54,13 +56,6 @@ private void onEntryFound(File parent,
throw new Exception("Create dir fail");
}
} else {
// TODO: support .dz file. issue #2
if (fileName.endsWith(".dz")) {
StringBuilder msg = new StringBuilder();
msg.append("Haven't support .dz (dictzip) file so far.\n");
msg.append("please provide '.dict' to instead of '.dict.dz'");
throw new RuntimeException(msg.toString());
}
// write file
File out = new File(parent, fileName);

Expand All @@ -78,7 +73,21 @@ private void onEntryFound(File parent,
archive.set(FileSet.Type.IDX, out.getAbsolutePath());
} else if (fileName.endsWith(".ifo")) {
archive.set(FileSet.Type.IFO, out.getAbsolutePath());
} else if (fileName.endsWith(".dict.dz") || fileName.endsWith(".dict")) {
} else if (fileName.endsWith(".dict.dz")) {
// found a dz file, use gzip to extract again
String extractFileName = out.getAbsolutePath().replace(".dict.dz", ".dict");
FileInputStream fis = new FileInputStream(out);
GzipCompressorInputStream gis = new GzipCompressorInputStream(fis);
FileOutputStream fos2 = new FileOutputStream(extractFileName);
byte[] buffer = new byte[2048];
int r = 0;
while ((r = gis.read(buffer))!= -1) {
fos2.write(buffer, 0, r);
}
fis.close();
fos2.close();
archive.set(FileSet.Type.DICT, extractFileName);
} else if (fileName.endsWith(".dict")) {
archive.set(FileSet.Type.DICT, out.getAbsolutePath());
}
}
Expand Down

0 comments on commit ad70aec

Please sign in to comment.