Wednesday, March 16, 2016

Java ZIP Examples : How To Get Number Of Files In ZIP File

Intro


This examples, will demonstrates how to get number of files in zip file. To get total number of files, you can use ZipFile.size() method.

Examples


package com.freesamplecode.java.zip;

import java.io.IOException;
import java.util.zip.ZipFile;

public class GetNumberOfFilesDemo {
 public static void main(String[] args){
  ZipFile zipFile = null;
  
  try {
   zipFile = new ZipFile("D:/temp/data.zip");
   
   int numberOfFiles = zipFile.size();
   
   System.out.println("Number of files in zip file  "+zipFile.getName() + " are : "+numberOfFiles + " files");
   zipFile.close();
  } catch (IOException e) {
   
   e.printStackTrace();
  }
  
  
 }
}


Output


Number of files in zip file  D:\temp\data.zip are : 6 files

Screenshot

How To Get Number Of Files In ZIP File

Sunday, March 13, 2016

Java ZIP Examples : How To Read A Contents Of The ZIP File

Intro

This examples will demonstrates how to read a contents of the zip file using Java program.

Examples


package com.freesamplecode.java.zip;

import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class ReadZipFileDemo {
 public static void main(String[] args){
  File file = new File("D:/temp/data.zip");
  
  if(!file.exists()){
   System.out.println("File " +file.getName() +" does not exists");
  }
  
  System.out.println("Reading content a zip file....");
  System.out.println("File Name "+"\t"+"\t"+"\t"+"\t"+"Size");
  System.out.println("-----------------------------------------------");
  
  try {
   ZipFile zipFile = new ZipFile(file);
   Enumeration enumeration = zipFile.entries();
   
   while(enumeration.hasMoreElements()){
    ZipEntry ze = (ZipEntry) enumeration.nextElement();
    
    if(!ze.isDirectory()){
     System.out.println(ze.toString()+"\t"+"\t"+ze.getSize()+" byte");
     
    }
   }
  } catch (ZipException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
}


Output


Reading content a zip file....
File Name     Size
-----------------------------------------------
D:\temp\data\aku - Copy (2).txt  0 byte
D:\temp\data\aku - Copy (3).txt  0 byte
D:\temp\data\aku - Copy (4).txt  0 byte
D:\temp\data\aku - Copy (5).txt  0 byte
D:\temp\data\aku - Copy.txt  0 byte
D:\temp\data\aku.txt  0 byte


Screenshot

How To Read A Contents Of The ZIP File

Saturday, March 12, 2016

Java ZIP Examples : How To Compress Byte Array Using Deflater Class

Intro


This examples, will show how to compress a byte array using Deflater class.

Example


package com.freesamplecode.java.zip;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;

public class CompressByteArrayDeflaterDemo {
	public static void main(String[] args){
		String str = "This tutorials will demonstrates how to compress a byte array using Deflater class";
		
		//get byte a string str
		byte[] bytes = str.getBytes();
		
		//create a deflater object
		Deflater deflater = new Deflater();
		
		deflater.setInput(bytes);
		
		deflater.finish();
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
		
		byte[] buffers = new byte[2048];
		
		while(!deflater.finished()){
			int byteCompressed = deflater.deflate(buffers);
			baos.write(buffers, 0, byteCompressed);
		}
		
		byte[] byteCompressedArray = baos.toByteArray();
		
		System.out.println("Size of original byte array : "+bytes.length);
		System.out.println("Size of compressed byte array : "+byteCompressedArray.length);
	}
}


Output


Size of original byte array : 82
Size of compressed byte array : 76

Screenshot

How To Compress Byte Array Using Deflater Class

Java ZIP Example : How To Compress Or Zip A Directory Recursively

Intro


This example will demonstrates how to compress a directory recursively using ZipEntry class in the java.util.zip package.

Example


package com.freesamplecode.java.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDirectoryDemo {
 public static void main(String[] args){
  String directory = "D:/temp/data";
  String zipFile = "D:/temp/data.zip";
  
  ZipDirectoryDemo demo = new ZipDirectoryDemo();
  demo.zipDirectory(directory, zipFile);
 }
 
 private void zipDirectory(String dir, String zipFile){
  File directory = new File(dir);
  
  //get all files in the directory
  File[] listOfFiles = directory.listFiles();
  
  try {
   FileOutputStream fos = new FileOutputStream(zipFile);
   ZipOutputStream zos = new ZipOutputStream(fos);
   
   for(int i = 0; i < listOfFiles.length; i++){
    File file = listOfFiles[i];
    String absolutePath = file.getAbsolutePath();
    
    System.out.println("Adding file "+file.getAbsolutePath()+" to zip");
    
    ZipEntry zipEntry = new ZipEntry(absolutePath);
    zos.putNextEntry(zipEntry);
    
    FileInputStream fis = new FileInputStream(absolutePath);
    
    byte[] buffer = new byte[1024];
    
    int length = 0;
    
    while((length = fis.read(buffer)) > 0){
     zos.write(buffer);
    }
    
    zos.closeEntry();
    fis.close();
    
   }
   
   System.out.println("Zip processing completed");
   
   zos.close();
   fos.close();
   
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  

 }
 
 
}


Output


Adding file D:\temp\data\aku - Copy (2).txt to zip
Adding file D:\temp\data\aku - Copy (3).txt to zip
Adding file D:\temp\data\aku - Copy (4).txt to zip
Adding file D:\temp\data\aku - Copy (5).txt to zip
Adding file D:\temp\data\aku - Copy.txt to zip
Adding file D:\temp\data\aku.txt to zip
Zip processing completed

Screenshot

How To Compress Or Zip A Directory Recursively