|
-
Apr 21st, 2003, 12:06 PM
#1
Thread Starter
Dazed Member
Compression?
Ive been checking out some of the compression classes in java.util.zip and i was wondering if any extra information is added to a file when it is compressed. I slapped the following code together which just compresses and decompresses files from the command line. It seems that the more a file is compressed the larger it becomes. I took a file starting at 143 bytes and compressed it down to 72 bytes but after that each subsequent compression inflated the file size. 143,72,83,94,105,114. Now when a noncompressed file is attempted to be decompressed a java.util.zip.ZipException incorrect header check is thrown. So does file compression add headers to the file? It seems so.
Code:
import java.io.*;
import java.util.zip.*;
public class Compression{
public static void main(String[] args){
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
for(;;){
try{
System.out.println("enter mode" + " -c Or -d");
String mode = buff.readLine();
System.out.println("enter file");
String file = buff.readLine();
if(mode.equals("") || file.equals("")){
System.out.println("enter mode file");
System.exit(0);
}
if(mode.equals("-c")){
compress(file);
}
else if(mode.equals("-d")){
decompress(file);
}
else{
System.out.println("Correct mode needed");
}
System.out.println("Like to continue? no to exit");
String con = buff.readLine();
if(con.equals("no")) break;
}catch(IOException e){;}
}
}
public static void compress(String file){
File f = new File(file);
FileInputStream is = null;
DeflaterOutputStream dos = null;
try{
is = new FileInputStream(f);
byte[] buffer = new byte[(int)f.length()];
while(true){
int bytesread = is.read(buffer);
if(bytesread < 0){
is.close();
break;
}
}
dos = new DeflaterOutputStream(new FileOutputStream(f));
dos.write(buffer);
System.out.println("File compressed");
}catch(Exception e){System.err.println(e);}
finally{
try{
dos.flush();
dos.close();
}catch(Exception ie){System.err.println(ie);}
}
}
public static void decompress(String file){
File f = new File(file);
FileOutputStream fos = null;
InflaterInputStream iis = null;
try{
iis = new InflaterInputStream(new FileInputStream(f));
byte[] buffer = new byte[(int)f.length()];
while(true){
int bytesread = iis.read(buffer);
if(bytesread < 0){
iis.close();
break;
}
}
fos = new FileOutputStream(f);
fos.write(buffer);
System.out.println("File decompressed");
}catch(Exception ie){System.err.println(ie);}
finally{
try{
fos.flush();
fos.close();
}catch(Exception ie){System.err.println(ie);}
}
}
}
Last edited by Dilenger4; Apr 22nd, 2003 at 03:44 AM.
-
Apr 21st, 2003, 01:11 PM
#2
Hyperactive Member
Yeah, stuff like zip version, dictionary used etc is added as a header.
-
Apr 21st, 2003, 10:52 PM
#3
Thread Starter
Dazed Member
Interesting. Id like to try it out on larger files to see how far down the file is compacted.
-
Apr 22nd, 2003, 03:48 AM
#4
The header is one of two reasons why the file gets larger if you compress already compressed content.
The other lies in the compression algorithm.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|