java.lang.NullPointerException
at javaapplication1.CompresorArchivos.Comprimir(Compr esorArchivos.java:47)
at javaapplication1.CompresorArchivos.main(CompresorA rchivos.java:88)
Me estoy volviendo loco, en que falla el programa? como lo arreglo? Muchas gracias por leerme
Cita:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author albertos
*/
import java.util.zip.*;
import java.io.*;
public class CompresorArchivos {
private static final int TAMANO_BYTES = 1024;
public void Comprimir(String pFile, String pZipFile) throws Exception {
// objetos en memoria
FileInputStream fis = null;
FileOutputStream fos = null;
ZipOutputStream zipos = null;
// buffer
byte[] buffer = new byte[TAMANO_BYTES];
try {
// fichero a comprimir
fis = new FileInputStream(pFile);
// fichero contenedor del zip
fos = new FileOutputStream(pZipFile);
// fichero comprimido
zipos = new ZipOutputStream(fos);
ZipEntry zipEntry = new ZipEntry(pFile);
zipos.putNextEntry(zipEntry);
int len = 0;
// Comprimir
while ((len = fis.read(buffer, 0, TAMANO_BYTES)) != -1)
zipos.write(buffer, 0, len);
// volcar la memoria al disco
zipos.flush();
} catch (Exception e) {
throw e;
} finally {
// cerramos los files
zipos.close();
fis.close();
fos.close();
} // end try
} // end Comprimir
public void Descomprimir(String pZipFile, String pFile) throws Exception {
BufferedOutputStream bos = null;
FileInputStream fis = null;
ZipInputStream zipis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(pZipFile);
zipis = new ZipInputStream(new BufferedInputStream(fis));
if (zipis.getNextEntry() != null) {
int len = 0;
byte[] buffer = new byte[TAMANO_BYTES];
fos = new FileOutputStream(pFile);
bos = new BufferedOutputStream(fos, TAMANO_BYTES);
while ((len = zipis.read(buffer, 0, TAMANO_BYTES)) != -1)
bos.write(buffer, 0, len);
bos.flush();
} else {
throw new Exception("El zip no contenia fichero alguno");
} // end if
} catch (Exception e) {
throw e;
} finally {
bos.close();
zipis.close();
fos.close();
fis.close();
} // end try
} // end Descomprimir
// ejemplo de uso
public static void main(String[] args) throws Exception {
try {
CompresorArchivos arch = new CompresorArchivos();
arch.Comprimir("devtroce.jpg", "devtroce.zip");
System.out.println("Comprimido!");
arch.Descomprimir("devtroce.zip", "new_devtroce.jpg");
System.out.println("Descomprimido!");
} catch (Exception e) {
e.printStackTrace();
}
}
}// end class
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
/**
*
* @author albertos
*/
import java.util.zip.*;
import java.io.*;
public class CompresorArchivos {
private static final int TAMANO_BYTES = 1024;
public void Comprimir(String pFile, String pZipFile) throws Exception {
// objetos en memoria
FileInputStream fis = null;
FileOutputStream fos = null;
ZipOutputStream zipos = null;
// buffer
byte[] buffer = new byte[TAMANO_BYTES];
try {
// fichero a comprimir
fis = new FileInputStream(pFile);
// fichero contenedor del zip
fos = new FileOutputStream(pZipFile);
// fichero comprimido
zipos = new ZipOutputStream(fos);
ZipEntry zipEntry = new ZipEntry(pFile);
zipos.putNextEntry(zipEntry);
int len = 0;
// Comprimir
while ((len = fis.read(buffer, 0, TAMANO_BYTES)) != -1)
zipos.write(buffer, 0, len);
// volcar la memoria al disco
zipos.flush();
} catch (Exception e) {
throw e;
} finally {
// cerramos los files
zipos.close();
fis.close();
fos.close();
} // end try
} // end Comprimir
public void Descomprimir(String pZipFile, String pFile) throws Exception {
BufferedOutputStream bos = null;
FileInputStream fis = null;
ZipInputStream zipis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(pZipFile);
zipis = new ZipInputStream(new BufferedInputStream(fis));
if (zipis.getNextEntry() != null) {
int len = 0;
byte[] buffer = new byte[TAMANO_BYTES];
fos = new FileOutputStream(pFile);
bos = new BufferedOutputStream(fos, TAMANO_BYTES);
while ((len = zipis.read(buffer, 0, TAMANO_BYTES)) != -1)
bos.write(buffer, 0, len);
bos.flush();
} else {
throw new Exception("El zip no contenia fichero alguno");
} // end if
} catch (Exception e) {
throw e;
} finally {
bos.close();
zipis.close();
fos.close();
fis.close();
} // end try
} // end Descomprimir
// ejemplo de uso
public static void main(String[] args) throws Exception {
try {
CompresorArchivos arch = new CompresorArchivos();
arch.Comprimir("devtroce.jpg", "devtroce.zip");
System.out.println("Comprimido!");
arch.Descomprimir("devtroce.zip", "new_devtroce.jpg");
System.out.println("Descomprimido!");
} catch (Exception e) {
e.printStackTrace();
}
}
}// end class