Java


JAVA.STRUCT.ARCHIVE.EZF : Empty zip File Archived (Java)

Summary

An empty zip entry is added to a zip file.

The programmatic construction of zip or jar archives might be incorrect, when for instance empty entries get added to an archive. This checker looks for inconsistent operation in the creation of such archives.

Properties

Class Name Empty zip File Archived (Java)
Significance reliability
Mnemonic JAVA.STRUCT.ARCHIVE.EZF
Categories
CWE CWE:909 Missing Initialization of Resource
Availability Available for Java only.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Empty zip File Archived (Java)"

Example

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCreator {
  public static void main(String[] args) {
    try (ZipOutputStream jos = new JarOutputStream(new FileOutputStream(new File("archive.jar")))) {
      ZipEntry entry = new ZipEntry("doc.txt");
      jos.putNextEntry(entry);
      jos.closeEntry();  // Empty jar File Archived (Java) warning issued here 
    }
    catch (IOException e) {
      e.printStackTrace();
    }

    try (ZipOutputStream jos = new ZipOutputStream(new FileOutputStream(new File("archive.jar")))) {
      ZipEntry entry = new ZipEntry("doc.txt");
      jos.putNextEntry(entry);
      jos.closeEntry();  // Empty zip File Archived (Java) warning issued here 
    }
    catch (IOException e) {
      e.printStackTrace();
    }

    try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(new File("archive.jar")));
         FileInputStream fis = new FileInputStream(new File("test.txt"))) {

      ZipEntry entry = new ZipEntry("doc.txt");
      jos.putNextEntry(entry);

      byte[] buf = new byte[1024];
      int len;
      while((len = fis.read(buf)) > 0)
        jos.write(buf, 0, len);

      jos.closeEntry();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Resolution

Check if, for instance, empty entries are added to a jar or zip archive.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.