Results 1 to 4 of 4

Thread: Compression?

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

  2. #2
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    Yeah, stuff like zip version, dictionary used etc is added as a header.

  3. #3

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width