I made a java program to download a zipped file from a server from a URL (in this case, I am using localhost to test), but it is giving the following error:
Exception in thread "main" java.util.zip.ZipException: invalid entry compressed size (expected 5505 but got 7388 bytes).
The idea of the program is to download a zipped file and save it to the PC's disk (do not unzip, just save).
Inside the zipped file I put two txt files and a bmp image, nothing too big, just to test.
I think the error is not in the zip file, because I used winrar to compress it, so I think my program is wrong, but I can not figure out the error. Here is the code:
public static void main(String[] args) throws MalformedURLException, IOException {
URL url = new URL("http://localhost/zipado.zip"); //conecta com localhost e busca o arquivo a ser baixado
InputStream is = url.openStream(); // abre um fluxo de dados para baixar o arquivo
ZipInputStream zin = new ZipInputStream(is); // cria um fluxo para ler arquivos zipados
File f = new File("C:/saida/zipado.zip"); // cria um arquivo de saida
FileOutputStream fout = new FileOutputStream(f); // abre um fluxo para gravar os dados no disco
ZipOutputStream zos = new ZipOutputStream(fout); // cria um fluxo para zipar dados
while (true) {
ZipEntry ze = zin.getNextEntry(); // recebe os "entrys" do arquivos baixado
if(ze==null) // verifica se ja recebeu todos os "entrys"
break;
System.out.println("Unzipping " + ze.getName()); // apresenta os "entrys"
zos.putNextEntry(ze); // posiciona o próximo entry
for (int c = zin.read(); c != -1; c = zin.read()) {
zos.write(c); // escreve os dados no arquivo
}
zos.closeEntry(); // fecha o entry
}
zos.close(); // fecha para zipar dados
fout.close(); // fecha para gravar no disco
zin.close(); // fecha o fluxo de entrada
}
The error occurs when you finish picking up all the "entrys" in the following snippet:
zos.closeEntry();
I would like your help in solving this problem.