- Apache supports compression of files.
- The output compressed file can be :- tar,zip,jar,bzip2,pack200,xz
- It has Compressor and Archiver components.
- Apache compress library download link:-
Project Structure:-
Testing Apache Compress:-
package com.sandeep.apache.compress;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;
public class TestApacheCompress {
/**
* @param args
*/
public static void main(String[] args) {
FileOutputStream fos = null;
ZipOutputStream zos = null;
byte[] btempBuffer = new byte[1024];
try {
fos = new FileOutputStream("D:\MyDocuments\Resume\sandeepZippedDocs.zip");
zos = new ZipOutputStream(fos);
ZipEntry ze= new ZipEntry("sandeep_cv_blue_zip.doc");//output folder
zos.putNextEntry(ze);
//input file to compress
FileInputStream in = new FileInputStream("D:\MyDocuments\Resume\sandeep_cv_blue.doc");
int len;
//reading
while ((len = in.read(btempBuffer)) > 0) {
zos.write(btempBuffer, 0, len);
}
in.close();
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
zos.closeEntry();
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}