Results 1 to 4 of 4

Thread: Compression?

Threaded View

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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);}
        }
       }
      }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width