IMAGE COMPRESSION AND DECOMPRESSION USING JAVA APPLICATIONS

format of compressions
Many sources of information contain redundant data or data that adds little to the stored information. This results in tremendous amounts of data being transferred between client and server applications or computers in general. The obvious solution to the problems of data storage and information transfer is to install additional storage devices and expand existing communication facilities.
To do so, however, requires an increase in an organization's operating costs. One method to alleviate a portion of data storage and information transfer is through the representation of data by more efficient code. This is an introduction to data compression and decompression, and shows how to compress and decompress data, efficiently and conveniently, from within your Java applications using the java.util.zip package.
data

There are many benefits to data compression. The main advantage of it, however, is to reduce storage requirements. Also, for data communications, the transfer of compressed data over a medium results in an increase in the rate of information transfer. Note that data compression can be implemented on existing hardware by software or through the use of special hardware devices that incorporate compression techniques.

Decompressing and Extracting Data from a ZIP file

The java.util.zip package provides classes for data compression and decompression. Decompressing a ZIP file is a matter of reading data from an input stream. The java.util.zip package provides a Zip Input Stream class for reading ZIP files. A Zip Input Stream can be created just like any other input stream. For example, the following segment of code can be used to create an input stream for reading data from a ZIP file format:

FileInputStream fis = new FileInputStream("figs.zip"); ZipInputStream zin = new    ZipInputStream(new BufferedInputStream(fis));
Once a ZIP input stream is opened, you can read the zip entries using the getNextEntry method which returns a ZipEntry object. If the end-of-file is reached, getNextEntry returns null:

ZipEntry entry;
while((entry = zin.getNextEntry()) != null) {
   // extract data
   // open output streams
}

Compressing and Archiving Data in a ZIP File

The ZipOutputStream can be used to compress data to a ZIP file. The ZipOutputStream writes data to an output stream in a ZIP format. There are a number of steps involved in creating a ZIP file.

The first step is to create a ZipOutputStream object, to which we pass the output stream of the file we wish to write to. Here is how you create a ZIP file entitled "myfigs.zip":
FileOutputStream dest = new
  FileOutputStream("myfigs.zip");
ZipOutputStream out = new
  ZipOutputStream(new BufferedOutputStream(dest));
Once the target zip output stream is created, the next step is to open the source data file. In this example, source data files are those files in the current directory. The list command is used to get a list of files in the current directory:
File f = new File(".");
String files[] = f.list();
for (int i=0; i<files.length; i++) {
   System.out.println("Adding: "+files[i]);
   FileInputStream fi = new FileInputStream(files[i]);
   // create zip entry
   // add entries to ZIP file
}