Use of setComment and getComment of ZipEntry.


 

Use of setComment and getComment of ZipEntry.

In this tutorial you will see how to use Use of setComment and getComment of ZipEntry.

In this tutorial you will see how to use Use of setComment and getComment of ZipEntry.

Use of setComment and getComment of ZipEntry.

In this tutorial, we will see the use of setComment and getComment method of ZipEntry 
class. The ZipEntry class is used show available entries in a ZIP file. The ZipEntry class is
 available in java.util.zip package.

In this Example, we will get the comment of entry with the help of getComment() method, 
if there is no comment on entry it returns null. The setComment() method set the comment
 on zip file entry. The getName method of ZipEntry class return the name of associated entry. 

About ZIPEntry API:

The java.util.zip.ZipEntry class extends java.lang.Object class. It provides the following methods:

Return Type Method Description 
String getComment() The method getComment() return comment of entry if not exists the returns null.
void setComment(String name) The setComment() method set comment string on entry.
String getName() The method getName() returns name of the associated entry.

code

import java.io.*;
import java.util.zip.*;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.Enumeration;
import java.io.IOException;

public class ZipGetComment {
  public static void main(String args[]) {
    try {
      ZipFile zipName = new ZipFile("test.zip");
      Enumeration enumEntries = zipName.entries();
      while (enumEntries.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntryenumEntries.nextElement();
        String zipEntryName = zipEntry.getName();
        String comment1 = zipEntry.getComment();
System.out.println("Name of Zip entry : " + zipEntryName);
        System.out.println("Comment on " + zipEntryName
            " entry before set comment : " + comment1);
        zipEntry.setComment("This file store name");
        String comment = zipEntry.getComment();
System.out.println("Name of Zip entry : " + zipEntryName);
        System.out.println("Comment on " + zipEntryName
            " entry after set comment : " + comment);
      }
      zipName.close();
    catch (IOException ioe) {
      System.out.println("Error opening zip file" + ioe);
    }
  }
}

Following is the output if you run the application:

C:\>java ZipGetComment
Name of Zip entry : bharat.txt
Comment on bharat.txt entry before set comment : null
Name of Zip entry : bharat.txt
Comment on bharat.txt entry after set comment : This file store name

Download this code

Ads