ecco il codice:
codice:
package zipper;
import java.util.zip.*;
import java.io.*;
public class Zippa {
public Zippa(String dir, String zip){
try{
zipDirectory(dir, zip);
}
catch(Exception e){
}
}
public void zipDirectory(String dir, String zipFile)throws Exception{
File d = new File(dir);
if(!d.isDirectory()){
System.out.println(dir+" non è una directory!");
}
else{
String[] entries = d.list();
System.out.println(d.list());
byte[] buffer = new byte[1024]; // Create a buffer for copying
int bytes_read;
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i = 0; i < entries.length; i++) {
System.out.println(i);
File file = new File(d, entries[i]);
System.out.println(d.getName());
if (file.isDirectory()) ;
FileInputStream in = new FileInputStream(file);
ZipEntry entry = new ZipEntry(file.getPath());
out.putNextEntry(entry);
while((bytes_read = in.read(buffer)) != -1)
out.write(buffer, 0, bytes_read);
in.close();
}
out.close();
}
}
public static void main(String args[]){
String file ="service.exe";
String dir="C:/apt";
String fileName="aprrrt.zip";
new Zippa(dir, fileName);
}
}
PS: Quando posti il codice , usa il tag [CODE]!