Menu



Java Program for Convert File To ZIP file


FileZip.java

import java.io.BufferedOutputStream;
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 FileZip {
public void zipFile(String filePath){
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream("d:/AcceleratorMonitor.zip");
zipOut = new ZipOutputStream(new BufferedOutputStream(fos));
File input = new File(filePath);
fis = new FileInputStream(input);
ZipEntry ze = new ZipEntry(input.getName());
System.out.println("Zipping the file: "+input.getName());
zipOut.putNextEntry(ze);
byte[] tmp = new byte[4*1024];
int size = 0;
while((size = fis.read(tmp)) != -1){
zipOut.write(tmp, 0, size);
}
zipOut.flush();
zipOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(fos != null) fos.close();
if(fis != null) fis.close();
} catch(Exception ex){
}
}
}
public static void main(String a[]){
FileZip mfe = new FileZip();
mfe.zipFile("d:/AcceleratorMonitor.java");
}
}

JAVA program to convert multiple file to zip

MultipleFileZip.java

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class MultipleFileZip {
public void zipFiles(List files){
FileOutputStream fos = null;
ZipOutputStream zipOut = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream("d:/testing.zip");
zipOut = new ZipOutputStream(new BufferedOutputStream(fos));
for(String filePath:files){
File input = new File(filePath);
fis = new FileInputStream(input);
ZipEntry ze = new ZipEntry(input.getName());
System.out.println("Zipping the file: "+input.getName());
zipOut.putNextEntry(ze);
byte[] tmp = new byte[4*1024];
int size = 0;
while((size = fis.read(tmp)) != -1){
zipOut.write(tmp, 0, size);
}
zipOut.flush();
fis.close();
}
zipOut.close();
System.out.println("Done... Zipped the files...");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(fos != null) fos.close();
} catch(Exception ex){
}
}
}
public static void main(String a[]){
MultipleFileZip mfe = new MultipleFileZip();
List files = new ArrayList();
files.add("d:/Main.java");
files.add("d:/MyDeadlock.java");
files.add("d:/StackSort.java");
mfe.zipFiles(files);
}
}










Connection failed: SQLSTATE[28000] [1045] Access denied for user 'studentdb'@'localhost' (using password: YES)

Subscribe