All notable changes to this project will be documented in this file.
## [Unreleased]
+### Add
+- Add test option for DictZip CLI.
+
### Changed
- README: there is no test dependency for commons-io.
package org.dict.zip.cli;
+import org.dict.zip.DictZipFileUtils;
import org.dict.zip.DictZipHeader.CompressionLevel;
import java.io.File;
commandLine.options.setKeep(true);
dict = new DictData(fName, null);
dict.printHeader();
+ } else if (commandLine.options.isTest()) {
+ boolean result = false;
+ try {
+ result = DictZipFileUtils.checkDictZipInputStream(fName);
+ } catch (IOException e) {
+ System.err.println(e.getMessage());
+ System.exit(2);
+ }
+ if (result) {
+ System.exit(0);
+ } else {
+ System.err.println(messages.getString("main.test.error"));
+ System.exit(1);
+ }
} else if (commandLine.options.isDecompress()) {
String extractFile = DictZipUtils.uncompressedFileName(fName);
long start = commandLine.options.getStart();
main.delete.error=File delete failed.
dictzip.header.title=type\tcrc \tdate \ttime \tchunks\tsize\tcompr.\tuncompr.\tratio\tname
+
+main.test.error=File integrity error.
\ No newline at end of file
/**
* Created by Hiroshi Miura on 16/04/09.
*/
-class DictZipFileUtils {
+public class DictZipFileUtils {
+
+
/**
* Reads unsigned byte.
*
return result;
}
+ /**
+ * Check gzip member stream w/ CRC and length in trailer.
+ * @throws IOException when CRC error or total length error.
+ */
+ public static boolean checkDictZipInputStream(String fileName) throws IOException {
+ boolean result;
+ try (DictZipInputStream dzin = new DictZipInputStream(new RandomAccessInputStream(fileName, "r"))) {
+ result = checkDictZipInputStream(dzin);
+ dzin.close();
+ }
+ return result;
+ }
+
+ /**
+ * Check gzip member stream w/ CRC and length in trailer.
+ * @throws IOException when CRC error or total length error.
+ */
+ public static boolean checkDictZipInputStream(DictZipInputStream in) throws IOException {
+ final int BUF_LEN = 65536;
+ byte[] tmpBuf = new byte[BUF_LEN];
+ in.seek(0);
+ long readLen = 0;
+ while (readLen < in.getLength()) {
+ int len = in.read(tmpBuf, 0, BUF_LEN);
+ if (len < 0) {
+ break;
+ }
+ readLen += len;
+ }
+ if (readLen != in.getLength()) {
+ return false;
+ }
+ return true;
+ }
+
private DictZipFileUtils() {
}
}
}
/**
+ * Get member length.
+ * @return gzip member length
+ */
+ public long getMemberLength() {
+ return (long) offsets[chunkCount - 1] + chunks[chunkCount - 1] + 8 + 2;
+ }
+
+ /**
* Get header length.
* @return header length
*/
public DictZipInputStream(final RandomAccessInputStream in, final int size) throws IOException {
super(in, new Inflater(true), size);
header = readHeader();
+ readTrailer();
}
/**
}
/**
- * Check gzip member trailer; CRC and length.
- * @throws IOException when CRC error or total length error.
- */
- public void checkTrailer() throws IOException {
- InputStream in = this.in;
- int num = inf.getRemaining();
- if (num > 0) {
- in = new SequenceInputStream(
- new ByteArrayInputStream(buf, len - num, num), in);
- }
- long val = crc.getValue();
- long crcValue = DictZipFileUtils.readUInt(in);
- if (crcValue != val) {
- throw new IOException(MessageFormat
- .format("Incorrect CRC: Computed CRC = %8x / From input %8x", val, crcValue));
- }
- long total = inf.getTotalOut();
- long trailerTotal = DictZipFileUtils.readUInt(in);
- if (trailerTotal != total) {
- throw new IOException(MessageFormat
- .format("False number of uncompressed bytes: Computed size =%d / From input %d",
- total, trailerTotal));
- }
- }
-
- /**
* Reads GZIP member trailer.
* @throws java.io.IOException If file I/O error
*/
- public void readTrailer() throws IOException {
+ void readTrailer() throws IOException {
if (in instanceof RandomAccessInputStream) {
RandomAccessInputStream rain = (RandomAccessInputStream) in;
compLength = rain.getLength();
import org.testng.annotations.Test;
import java.io.File;
+import java.io.IOException;
import static org.testng.Assert.*;
File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
assertFalse(DictZipFileUtils.isFileBinaryEquals(firstFile, secondFile, 0, 64));
}
+
+ @Test
+ public void testCheckDictZipInputStream_string() throws Exception {
+ System.out.println("checkDictZipInputStream_string");
+ String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
+ assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile));
+ }
+
+ @Test
+ public void testCheckDictZipInputStream_stringNoExist() throws Exception {
+ System.out.println("checkDictZipInputStream_string");
+ String targetFile = "false.dict.dz";
+ boolean result;
+ try {
+ DictZipFileUtils.checkDictZipInputStream(targetFile);
+ result = false;
+ } catch (IOException e) {
+ // expected.
+ result = true;
+ }
+ assertTrue(result);
+ }
+
+ @Test
+ public void testCheckDictZipInputStream() throws Exception {
+ System.out.println("checkDictZipInputStream");
+ String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
+ try (DictZipInputStream dzin = new DictZipInputStream(new
+ RandomAccessInputStream(targetFile, "r"))) {
+ assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin));
+ dzin.close();
+ }
+ }
}
\ No newline at end of file
}
/**
+ * Test of getMemberLength method.
+ */
+ @Test
+ public void testGetMemberLength() throws Exception {
+ System.out.println("getMemberLength");
+ String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
+ DictZipHeader result = DictZipHeader.readHeader(dataFile);
+ assertEquals(result.getMemberLength(), 136856);
+ }
+
+ /**
* Test of writeHeader method.
*
* @throws Exception if file I/O error occurred.