1+ package project0805006 ;
2+
3+ import java .util .*;
4+ import java .io .*;
5+
6+
7+
8+
9+
10+ public class Lunzipping
11+ {
12+
13+ public static int bitsz1 ;
14+
15+ //byte er string representation in eight digit
16+ public static String bttost []=new String [256 ];
17+ public static String big1 ;
18+
19+
20+
21+
22+
23+ /*============================================================
24+ * make all the binary to string conversion for 8 bits
25+ *=============================================================*/
26+ public static void pre ()
27+ {
28+ int i ,j ;
29+ String r1 ;
30+ bttost [0 ]="0" ;
31+ for (i =0 ;i <256 ;i ++)
32+ {
33+ r1 ="" ;
34+ j =i ;
35+ if (i !=0 ) bttost [i ]="" ;
36+ while (j !=0 )
37+ {
38+ if ((j %2 )==1 ) bttost [i ]+="1" ;
39+ else bttost [i ]+="0" ;
40+ j /=2 ;
41+ }
42+ for (j =bttost [i ].length ()-1 ;j >=0 ;j --)
43+ {
44+ r1 +=bttost [i ].charAt (j );
45+ }
46+ while (r1 .length ()<8 )
47+ {
48+ r1 ="0" +r1 ;
49+ }
50+ bttost [i ]=r1 ;
51+ }
52+ }
53+ /*=========================================================================*/
54+
55+
56+
57+
58+ /*==========================================================================
59+ * byte to int conversion
60+ *=========================================================================*/
61+ public static int btoi (Byte bt )
62+ {
63+ int ret =bt ;
64+ if (ret <0 )
65+ ret +=256 ;
66+ return ret ;
67+
68+ }
69+
70+ /**======================================================================*/
71+
72+
73+
74+ /*==========================================================================
75+ * byte to int conversion
76+ *=========================================================================*/
77+ public static int stoi (String s )
78+ {
79+ int ret =0 ,i ;
80+ for (i =0 ;i <s .length ();i ++)
81+ {
82+ ret *=2 ;
83+ if (s .charAt (i )=='1' ) ret ++;
84+ }
85+ return ret ;
86+ }
87+ /**======================================================================*/
88+
89+
90+
91+ public static void Lunzip (String fileis )
92+ {
93+ int k ;
94+ int dictSize = 256 ;
95+ int mpsz =256 ;
96+ String ts ;
97+ Map <Integer ,String > dictionary = new HashMap <Integer ,String >();
98+ for (int i = 0 ; i < 256 ; i ++)
99+ dictionary .put (i , "" + (char )i );
100+
101+ String fileos =filei .substring (0 ,filei .length ()-6 );
102+
103+
104+ File filei =null ,fileo =null ;
105+ filei =new File (fileis );
106+ fileo =new File (fileos );
107+ try
108+ {
109+ FileInputStream file_input = new FileInputStream (filei );
110+ DataInputStream data_in = new DataInputStream (file_input );
111+ FileOutputStream file_output = new FileOutputStream (fileo );
112+ DataOutputStream data_out = new DataOutputStream (file_output );
113+
114+
115+ Byte c ;
116+ bitsz1 =data_in .readInt ();
117+
118+
119+
120+ while (true )
121+ {
122+ try
123+ {
124+ c =data_in .readByte ();
125+ big1 +=bttost [btoi (c )];
126+ if (big1 .length ()>=bitsz1 )
127+ break ;
128+ }
129+ catch (EOFException eof ){System .out .println ("End of File" );break ;}
130+ }
131+
132+
133+ if (big1 .length ()>=bitsz1 )
134+ {
135+ k =stoi (big1 .substring (0 ,bitsz1 ));
136+ big1 =big1 .substring (bitsz1 ,big1 .length ());
137+ }
138+ else
139+ {
140+ data_in .close ();
141+ data_out .close ();
142+ return ;
143+ }
144+
145+
146+ String w = "" + (char )k ;
147+
148+
149+ data_out .writeBytes (w );
150+ //System.out.println(w);
151+
152+
153+ while (true )
154+ {
155+ try
156+ {
157+ while (big1 .length ()<bitsz1 )
158+ {
159+ c =data_in .readByte ();
160+ big1 +=bttost [btoi (c )];
161+ }
162+ k =stoi (big1 .substring (0 ,bitsz1 ));
163+ big1 =big1 .substring (bitsz1 ,big1 .length ());
164+
165+
166+ String entry ="" ;
167+ if (dictionary .containsKey (k ))
168+ {
169+
170+ entry = dictionary .get (k );
171+ }
172+ else if (k == dictSize )
173+ {
174+ entry = w + w .charAt (0 );
175+
176+ }
177+ data_out .writeBytes (entry );
178+
179+ if (mpsz <100000 )
180+ {
181+ ts =w + entry .charAt (0 );
182+ dictionary .put (dictSize ++,ts );
183+ mpsz +=ts .length ();
184+ }
185+ w = entry ;
186+ }
187+ catch (EOFException eof ){System .out .println ("End of File" );break ;}
188+ }
189+ data_in .close ();
190+ data_out .close ();
191+ file_input .close ();
192+ file_output .close ();
193+ }
194+ catch (IOException e ) {System .out .println ("IO exception = " + e );}
195+
196+
197+ filei =null ;
198+ fileo =null ;
199+
200+ }
201+
202+
203+
204+
205+
206+ public static void beginLunzipping (String arg1 )
207+ {
208+ big1 ="" ;
209+ bitsz1 =0 ;
210+ pre ();
211+ Lunzip (arg1 );
212+ big1 ="" ;
213+ bitsz1 =0 ;
214+ }
215+
216+
217+
218+
219+ /*
220+ public static void main(String[] args)
221+ {
222+ big1="";
223+ bitsz1=0;
224+ pre();
225+ Lunzip("in.txt.LmZWp");
226+ big1="";
227+ bitsz1=0;
228+ }
229+ */
230+ }
0 commit comments