Adam Smith Adam Smith - 1 year ago 70
Java Question

Java not writing to zip file

I have the following java method which I'm trying to use to create a zip file, which has an existing file written into it (the zip file has the same name, just with the .log extension replaced with .zip) . The zip file is created successfully, however the file is not inside it when it completes.

Here is my code:

private static void zipFile(File fileToZip) {

final int bufferSize = 2048;
File zipFile = new File(fileToZip.getAbsolutePath().replaceAll(".log", ".zip"));

try (FileOutputStream fos = new FileOutputStream(zipFile.getAbsolutePath());
FileInputStream fis = new FileInputStream(fileToZip);
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
BufferedInputStream origin = new BufferedInputStream(fis, bufferSize)) {

ZipEntry ze = new ZipEntry(fileToZip.getAbsolutePath());
byte[] data = new byte[bufferSize];
int count;
while ((count =, 0, bufferSize)) != -1) {"WRITING!!!");
zos.write(data, 0, count);

} catch (IOException e) {
LOGGER.error("Error: ", e);


Any ideas? :)

Answer Source


ZipEntry ze = new ZipEntry(fileToZip.getAbsolutePath());


ZipEntry ze = new ZipEntry(fileToZip.getName());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download