Java Reference
In-Depth Information
1
class Hzip
2
{
3
public static void compress( String inFile ) throws IOException
4
{
5
String compressedFile = inFile + ".huf";
6
InputStream in = new BufferedInputStream(
7
new FileInputStream( inFile ) );
8
OutputStream fout = new BufferedOutputStream(
9
new FileOutputStream( compressedFile ) );
10
HZIPOutputStream hzout = new HZIPOutputStream( fout );
11
int ch;
12
while( ( ch = in.read( ) ) != -1 )
13
hzout.write( ch );
14
in.close( );
15
hzout.close( );
16
}
17
18
public static void uncompress( String compressedFile ) throws IOException
19
{
20
String inFile;
21
String extension;
22
23
inFile = compressedFile.substring( 0, compressedFile.length( ) - 4 );
24
extension = compressedFile.substring( compressedFile.length( ) - 4 );
25
26
if( !extension.equals( ".huf" ) )
27
{
28
System.out.println( "Not a compressed file!" );
29
return;
30
}
31
32
inFile += ".uc"; // for debugging, to not clobber original
33
InputStream fin = new BufferedInputStream(
34
new FileInputStream( compressedFile ) );
35
DataInputStream in = new DataInputStream( fin );
36
HZIPInputStream hzin = new HZIPInputStream( in );
37
38
OutputStream fout = new BufferedOutputStream(
39
new FileOutputStream( inFile ) );
40
int ch;
41
while( ( ch = hzin.read( ) ) != -1 )
42
fout.write( ch );
43
44
hzin.close( );
45
fout.close( );
46
}
47
}
figure 12.25
A simple
main
for file compression and uncompression
Search WWH ::
Custom Search