Before Something to Achieve First Learn How to Reach There |
package com.lara;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipMultipleFiles
{
public static void main(String[] args)
{
File dir = new File("D:\\Mukesh Snaps At Waynad\\Waynad Scenarious");
File[] listFiles = new File[(int)dir.length()];
listFiles = dir.listFiles();
System.out.println(listFiles.length);
String[] fileLoc = new String[listFiles.length];
String[] fileName = new String[listFiles.length];
for (int i = 0; i < fileLoc.length; i++)
{
fileLoc[i] = listFiles[i].getAbsolutePath();
}
for (int i = 0; i < fileName.length; i++)
{
fileName[i] = listFiles[i].getName();
}
try
{
String zippedFile = "D:\\ZippedFiles.zip";
byte[] buffer = new byte[1024];
FileOutputStream fout = new FileOutputStream(zippedFile);
ZipOutputStream zout = new ZipOutputStream(fout);
for (int i = 0; i < fileLoc.length; i++)
{
System.out.println("Adding : "+fileName[i]);
FileInputStream fin = new FileInputStream(fileLoc[i]);
zout.putNextEntry(new ZipEntry(fileName[i]));
int length;
while((length = fin.read(buffer))>0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
fin.close();
}
zout.close();
System.out.println("Zip File has been Created Successfully.....");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
No comments:
Post a Comment