77import java .nio .file .Files ;
88import java .nio .file .Path ;
99import java .nio .file .Paths ;
10- import java .util .BitSet ;
11- import java .util .HashMap ;
12- import java .util .List ;
13- import java .util .Map ;
10+ import java .util .*;
1411
1512import static HuffmanCompression .SerializeHuffmanTable .deserializeFromByteArray ;
1613import static HuffmanCompression .SerializeHuffmanTable .serializeToByteArray ;
@@ -91,29 +88,36 @@ private static String getFileExtension(File file) {
9188 }
9289 }
9390
91+ private static void writeData (File file , byte [] dataBytes , String extension ) throws IOException {
92+ File compressed = new File (file .getAbsolutePath () + extension );
93+ compressed .createNewFile ();
94+ Path compressedPath = Paths .get (compressed .getAbsolutePath ());
95+ Files .write (compressedPath , dataBytes );
96+ }
97+
98+ private static void writeMeta (File file , byte [] dataBytes ) throws IOException {
99+ File compressed = new File (file .getAbsolutePath () + ".meta" );
100+ compressed .createNewFile ();
101+ Path compressedPath = Paths .get (compressed .getAbsolutePath ());
102+ Files .write (compressedPath , dataBytes );
103+ }
104+
94105 public static void writeFile (byte [] data , String path , String arg ) throws IOException , UnexpectedFileFormat {
95106 File file = new File (path );
96107
97108 if (arg .equals (COMPRESS_OPTION )) {
98- toFileWrite (file , data , COMPRESS_EXTENSION );
109+ writeData (file , data , COMPRESS_EXTENSION );
99110 }
100111
101112 if (arg .equals (DECOMPRESS_OPTION )) {
102113 if (!getFileExtension (file ).equals (COMPRESS_EXTENSION )) {
103114 throw new UnexpectedFileFormat ("File format is unexpected" );
104115 }
105- toFileWrite (file , data , DECOMPRESS_EXTENSION );
116+ writeData (file , data , DECOMPRESS_EXTENSION );
117+ writeMeta (file ,data );
106118 }
107119 }
108120
109-
110- private static void toFileWrite (File file , byte [] dataBytes , String extension ) throws IOException {
111- File compressed = new File (file .getAbsolutePath () + extension );
112- compressed .createNewFile ();
113- Path compressedPath = Paths .get (compressed .getAbsolutePath ());
114- Files .write (compressedPath , dataBytes );
115- }
116-
117121 public static List <String > readFileStrings (File file ) throws IOException {
118122 List <String > string = Files .readAllLines (Paths .get (file .getAbsolutePath ()));
119123 return string ;
@@ -123,6 +127,18 @@ public static byte[] readFileBytes(File file) throws IOException {
123127 return Files .readAllBytes (Paths .get (file .getAbsolutePath ()));
124128 }
125129
130+ public static List <byte []> readCompressedFiles (File file ) throws UnexpectedFileFormat , IOException {
131+ List <byte []> list = new ArrayList <>();
132+ File tableFile = new File (file .getAbsolutePath ()+".meta" );
133+ File dataFile = new File (file .getAbsolutePath ());
134+ if (!dataFile .exists () || !tableFile .exists ()){
135+ throw new UnexpectedFileFormat ("cant find table or meta file" );
136+ }
137+ list .add (Files .readAllBytes (Paths .get (tableFile .getAbsolutePath ())));
138+ list .add (Files .readAllBytes (Paths .get (dataFile .getAbsolutePath ())));
139+ return list ;
140+ }
141+
126142 public static byte [] getTableBytes (byte [] data ) {
127143 byte [] tableLength = new byte [4 ];
128144 System .arraycopy (data , 0 , tableLength , 0 , tableLength .length );
@@ -208,4 +224,5 @@ public int getLength() {
208224 }
209225
210226
227+
211228}
0 commit comments