/**
*
* @author jonathan-palomino.blogspot.com
*/
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.*;
import java.io.*;
public class Zipear {
public static void Comprimir(String args[]) throws IOException{
if (args.length < 2) {
System.err.println("use: Zip.zip file1 file2 file3");
System.exit(-1);
}
File zipFile = new File(args[0]);
if (zipFile.exists()) {
System.err.println("Archivo Zip existente seleccione otro");
System.exit(-2);
}
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
int bytesRead;
byte[] buffer = new byte[1024];
CRC32 crc = new CRC32();
for (int i=1, n=args.length; i < n; i++) {
String name = args[i];
System.out.println("--------"+name);
File file = new File(name);
if (!file.exists()) {
System.err.println("Skipping: " + name);
continue;
}
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
crc.reset();
while ((bytesRead = bis.read(buffer)) != -1) {
crc.update(buffer, 0, bytesRead);
}
bis.close();
// Reset to beginning of input stream
bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(name);
entry.setMethod(ZipEntry.STORED);
entry.setCompressedSize(file.length());
entry.setSize(file.length());
entry.setCrc(crc.getValue());
zos.putNextEntry(entry);
while ((bytesRead = bis.read(buffer)) != -1) {
zos.write(buffer, 0, bytesRead);
}
bis.close();
}
zos.close();
}
public static void main(String args[]) {
String argumentos[] = {"C:\\zip.zip","C:\\Test.pdf"};
try {
Comprimir(argumentos);
} catch (IOException ex) {
Logger.getLogger(Zipear.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Home »
» Zipear archivos
Zipear archivos
Para zipear archivos desde java les traigo la siguiente clase:






No hay comentarios:
Publicar un comentario