5 Replies Latest reply: Mar 11, 2013 11:02 PM by rp0428 RSS

    compression using deflater?

    Murray9654
      Hi I am using Deflater to compress the file but i could not compress at all. The following is my code
      package compress;
      
      import java.io.BufferedInputStream;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.util.zip.Deflater;
      import java.util.zip.DeflaterOutputStream;
      
      
      public class CompressingFile {
      
           
           public static void main(String[] args)throws Exception {
                File file=new File("d://training//mur.java");
                FileInputStream fis=new FileInputStream(file);
                BufferedInputStream bis=new BufferedInputStream(fis);
                
                File compressedFile=new File("d://training//mur_compressed.java");
                FileOutputStream fos=new FileOutputStream(compressedFile);
                Deflater de=new Deflater();
                de.setLevel(9);
                DeflaterOutputStream dos=new DeflaterOutputStream(fos,de);
                
                int a;
                while((a=bis.read())!=-1){
                     dos.write(a);
                }
                dos.close();
                bis.close();
      
           }
      
      }
      before compression i have checked the size of the file I am compressing and after compression i have checked the file size of the compressed file but both are same. Then what is the point of using Deflater.
        • 1. Re: compression using deflater?
          EJP
          What's in the file? Binary data doesn't compress well, and encrypted data not at all. Compression works best on text.
          • 2. Re: compression using deflater?
            rp0428
            >
            before compression i have checked the size of the file I am compressing and after compression i have checked the file size of the compressed file but both are same. Then what is the point of using Deflater.
            >
            How are you determining the size of the file before and after?

            How big is the file? There is some overhead for all files so if your source file is very small the size of the overhead may be more than what you save by compressing the file.

            If you just look at what the operating system shows it may be showing the number of bytes used on the file system. If the OS block size is 512 bytes then even a one byte file will use a full 512 byte block.

            Are you on windows? Open the file using WinZip or one of the other zip utilties. These will show you the uncompressed and compressed sizes.

            It would be very unusual for the before and after sizes to be exactly equal since there is some overhead

            One tip - don't use code like this:
                      de.setLevel(9);
            No one reading or maintaining your code is going to know what '9' means. You should use:
              de.setLevel(Deflater.BEST_COMPRESSION);
            That makes it clear what the compression setting is.

            Also, if you are always going to use the same compression method with this deflater you can just set it in the constructor.
            Deflater de=new Deflater(Deflater.BEST_COMPRESSION);
            You can still change it later if you want.

            See the 'Deflater' API
            http://docs.oracle.com/javase/6/docs/api/java/util/zip/Deflater.html#NO_COMPRESSION
            • 3. Re: compression using deflater?
              Murray9654
              I have used compression on the text file size which is of 101 bytes and after compression it is the same. but when i use compression on text file which is of 12 kb then the compressed file is of 3.4 kb. When i compressed the 1.51 mb jpg file then there is very slight difference.

              Mostly I want to use compression on image files such as jpg's. so what is best?
              • 4. Re: compression using deflater?
                EJP
                Mostly I want to use compression on image files such as jpg's.
                JPEG files already are compressed, and by a much cleverer algorithm than the Deflater.
                • 5. Re: compression using deflater?
                  rp0428
                  >
                  I have used compression on the text file size which is of 101 bytes and after compression it is the same. but when i use compression on text file which is of 12 kb then the compressed file is of 3.4 kb.
                  >
                  And that agrees with what I said above. There is overhead in the file; information about the type of compression for examples. You can determine the amount of overhead by compressing a 1 byte file. The resulting file size will ALL be overhead except for one byte.
                  >
                  When i compressed the 1.51 mb jpg file then there is very slight difference.

                  Mostly I want to use compression on image files such as jpg's. so what is best?
                  >
                  Answer this question: Suppose you compress file 'myFile' and get a file named 'myCompressedFile'. Now you compress 'myCompressedFile'. Do you think the file will get any smaller?

                  No - it will usually get bigger. It has already been compressed and now it can't be compressed anymore but you are adding more overhead bytes to it making it bigger.

                  You usually aren't going to save much, if anything, if you compress files that have already been compressed. You can run some tests using different file types to see if you get any useful compression. If you don't then don't bother compressiong those file types.