
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Zipingfile {
BufferedReader readkeyboard=new BufferedReader(new InputStreamReader(System.in));
public static int buffer = 5242890;
String source,detination;
long overallSize = 0;
public void zipingfile() {
try {
System.out.println("Enter source path :");
source=readkeyboard.readLine();
//creating ziping
File folder = new File(source);
if(folder.exists()) {
System.out.println("Enter Destination path :");
detination=readkeyboard.readLine();
File[] files = folder.listFiles();
File zipedfile=new File(detination);
byte binarybits[] = new byte[buffer];
FileOutputStream writefile = new FileOutputStream(zipedfile);
ZipOutputStream zip = new ZipOutputStream(writefile);
for (int i = 0; i < files.length; i++) {
//if (files[i] == null || !files[i].exists()|| files[i].isDirectory())
//System.out.println();
ZipEntry addFiles = new ZipEntry(files[i].getName());
addFiles.setTime(files[i].lastModified());
overallSize+=zipedfile.length();
//if(files[i].length() >= buffer) {
//continue;
//}
zip.putNextEntry(addFiles);
if(overallSize < buffer) {
//zip.putNextEntry(new ZipEntry(files[i].getName()));
FileInputStream readfile = new FileInputStream(files[i]);
while (true) {
System.out.println("infiniti loopppp");
int filelen = readfile.read(binarybits, 0, binarybits.length);
// System.out.println(">>>>>>>>"+filelen);
//if file is empty or >5mb
if (filelen <= 0)
break;
zip.write(binarybits, 0, filelen);
}
readfile.close();
}
else{
System.out.println("zip file morethan 5MB zip is not possible");
//zip=null;
/*for (File file : files)
{
// Delete each file
if (!file.delete())
{
// Failed to delete file
System.out.println("Failed to delete "+file);
}
} */
// zip.delete();
return;
}
}
System.out.println("Zip File is created successfully.");
System.out.println("File Name: " + zipedfile.getName());
System.out.println("Destination path : " + zipedfile.getPath());
//int size= zipedfile.length()*1024*1024;
System.out.println("File size: " + zipedfile.length() + "bytes ");
zip.close();
writefile.close();
}else {
System.out.println("file doesn't exist");
}
} catch(IOException io){
io.printStackTrace();
}
}
public static void main(String[] args) {
Zipingfile filezip=new Zipingfile();
filezip.zipingfile();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.