2 * Copyright (C) 2016 Hiroshi Miura
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package org.dict.zip.cli;
21 import java.io.EOFException;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.io.RandomAccessFile;
27 import java.text.Format;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.Locale;
31 import java.util.ResourceBundle;
32 import java.util.zip.Deflater;
35 import org.dict.zip.DictZipHeader;
36 import org.dict.zip.DictZipHeader.CompressionLevel;
37 import org.dict.zip.DictZipInputStream;
38 import org.dict.zip.DictZipOutputStream;
39 import org.dict.zip.RandomAccessInputStream;
40 import org.dict.zip.RandomAccessOutputStream;
44 * Dictionary data handling class.
45 * @author Hiroshi Miura
47 public class DictData {
49 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
50 .getBundle("org/dict/zip/cli/Bundle", Locale.getDefault());
52 private final String originalFileName;
53 private final String compressedFileName;
54 private static final int BUF_LEN = 58315;
57 * Default constructor for reader.
58 * @param originalFileName to handle
59 * @param compressedFileName to handle
61 public DictData(final String originalFileName, final String compressedFileName) {
62 this.originalFileName = originalFileName;
63 this.compressedFileName = compressedFileName;
67 * Print header information to STDOUT.
68 * @throws IOException when stdout is terminated.
70 public void printHeader() throws IOException {
71 File targetFile = new File(originalFileName);
72 RandomAccessFile targetRaFile = new RandomAccessFile(targetFile, "r");
73 try (RandomAccessInputStream in = new RandomAccessInputStream(targetRaFile);
74 DictZipInputStream din = new DictZipInputStream(in);) {
75 long uncomp = din.getLength();
76 long comp = din.getCompLength();
77 long crc = din.getCrc();
78 DictZipHeader header = din.readHeader();
79 String type = header.getType();
80 int chunkLength = header.getChunkLength();
81 int chunkCount = header.getChunkCount();
82 Date mtime = new Date(header.getMtime() * 1000);
83 String filename = header.getFilename();
84 Format timeFormatter = new SimpleDateFormat("MMMM dd, yyyy hh:mm:ss");
85 System.out.println(RESOURCE_BUNDLE.getString("dictzip.header.title"));
86 System.out.print(String.format("%s\t%08x\t%s\t", type, crc,
87 timeFormatter.format(mtime)));
88 System.out.print(String.format("%6d\t%d\t%d\t %d\t", chunkCount, chunkLength, comp,
90 System.out.println(String.format("%3.1f%%\t%s", (100.0 * comp) / uncomp, filename));
91 } catch (RuntimeException ex) {
98 * @throws IOException if file I/O error.
101 public void doZip(CompressionLevel level) throws IOException {
103 byte[] buf = new byte[BUF_LEN];
104 File originalFile = new File(originalFileName);
106 case BEST_COMPRESSION:
107 defLevel = Deflater.BEST_COMPRESSION;
110 defLevel = Deflater.BEST_SPEED;
112 case DEFAULT_COMPRESSION:
114 defLevel = Deflater.DEFAULT_COMPRESSION;
117 try (FileInputStream ins = new FileInputStream(originalFile);
118 DictZipOutputStream dout = new DictZipOutputStream(
119 new RandomAccessOutputStream(new RandomAccessFile(compressedFileName, "rws")),
120 BUF_LEN, originalFile.length(), defLevel)) {
122 while ((len = ins.read(buf, 0, BUF_LEN)) > 0) {
123 dout.write(buf, 0, len);
125 } catch (EOFException eof) {
132 * @param start start offset of data
133 * @param size size to retrieve
134 * @throws IOException if file I/O error.
136 public void doUnzip(final long start, final int size) throws IOException {
137 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(new
138 RandomAccessFile(new File(compressedFileName), "r")));
139 OutputStream unzipOut = new RandomAccessOutputStream(originalFileName, "rw")) {
140 byte[] buf = new byte[BUF_LEN];
144 while ((len = din.read(buf, 0, BUF_LEN)) > 0) {
145 unzipOut.write(buf, 0, len);
151 while (size - readSize > 0) {
152 if (size - readSize < BUF_LEN) {
153 len = din.read(buf, 0, size - readSize);
155 len = din.read(buf, 0, BUF_LEN);
158 unzipOut.write(buf, 0, len);
164 } catch (EOFException eof) {