Comprimir una carpeta

Hola!!!
Como puedo comprimir una carpeta en un archivo .zip desde Java??? Y descomprimirla???
Muchas gracias!!!

1 Respuesta

Respuesta
1
Aquí te paso el código necesario para comprimir una carpeta (y sus subdirectorios), hay también un main hecho para que veas como lo tienes que utilizar. Aún no he hecho el de descomprimir, en cuanto lo tenga te lo paso. Un saludo y espero que aún te valga:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream ;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.util.Date;
public class NZipCompresser {
private String m_basePath=null;
private File m_dir = null;
private String m_OutputFileName;
public static void main(String args[]){
NZipCompresser c=new NZipCompresser(new File(args[0]),args[1]);
try{
c.compress();
}
catch(Exception e){
System.out.println(e.toString());
}
System.out.println("Fin.....");
}
public NZipCompresser() {
}
// This class gets directory and compress it reqursively
//into zip file named outputFileName
public NZipCompresser(File directory,String outputFileName) {
m_dir = directory;
m_OutputFileName = outputFileName;
}
public void compress () throws Exception{
try {
FileOutputStream zipFilename = new FileOutputStream
(m_OutputFileName) ;
ZipOutputStream zipoutputstream = new
ZipOutputStream (zipFilename);
m_basePath = m_dir.getPath();
CompressDir (m_dir,zipoutputstream);
zipoutputstream.setMethod(ZipOutputStream.DEFLATED);
zipoutputstream.close();
}
catch (Exception e) {
throw new Exception ("Something wrong in compresser: " + e);
}
}
public void setDirectory (File dir) {
m_dir = dir;
}
public void setOutputFileName (String FileName) {
m_OutputFileName = FileName;
}
public File getDirectory () {
return m_dir;
}
public String getOutputFileName () {
return m_OutputFileName;
}
//Walker through directory structure
private void CompressDir (File f, ZipOutputStream
zipoutputstream) {
System.out.println(f);
if (f.isDirectory()) {
File [] files = f.listFiles();
int i;
for (i=0;i<files.length;i++) {
if (files.isDirectory()) { System.out.println("calling CompressOneDir with:"+files.getPath());
CompressDir (files,zipoutputstream);
}
if (files.isFile()) {
System.out.println("adding file:" + files.getPath());
addOneFile(files,zipoutputstream);
}
}
}
System.out.println("exiting:"+f);
}
//Actualy compress the file
private void addOneFile (File file, ZipOutputStream
zipoutputstream) {
ZipEntry zipentry = new ZipEntry(file.getPath().substring
(m_basePath.length()+1));
FileInputStream fileinputstream;
CRC32 crc32 = new CRC32();
byte [] rgb = new byte [1024];
int n;
//Compute CRC of input stream
try {
fileinputstream = new FileInputStream(file);
while ((n = fileinputstream.read(rgb)) > -1){
crc32.update(rgb, 0, n);
}
fileinputstream.close();
}
catch (Exception e) {
System.out.println("Error in computing CRC:");
e.printStackTrace();
}
//Set Up Zip Entry
zipentry.setSize(file.length());
zipentry.setTime(file.lastModified());
zipentry.setCrc(crc32.getValue());
//Write Data
try {
zipoutputstream.putNextEntry(zipentry);
fileinputstream = new FileInputStream(file);
while ((n = fileinputstream.read(rgb)) > -1) {
zipoutputstream.write(rgb, 0, n);
}
fileinputstream.close();
zipoutputstream.closeEntry();
}
catch (Exception ex) {
System.out.println("Error in writing data:");
ex.printStackTrace();
}
}
}

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas