Java by API/java.util.jar/JarEntry

Материал из Java эксперт
Версия от 14:43, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

JarEntry: getAttributes()

  
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
  public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
      JarEntry je = (JarEntry) e.nextElement();
      String name = je.getName();
      Attributes a = je.getAttributes();
      if (a != null) {
        Object[] nameValuePairs = a.entrySet().toArray();
        for (int j = 0; j < nameValuePairs.length; j++) {
          System.out.println(nameValuePairs[j]);
        }
      }
    }
  }
}





JarEntry: getComment()

  
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
  public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
      JarEntry je = (JarEntry) e.nextElement();
      String name = je.getName();
      long crc = je.getCrc();
      System.out.println("Its CRC is " + crc);
      String comment = je.getComment();
      if (comment != null && !comment.equals("")) {
        System.out.println(comment);
      }
      if (je.isDirectory()) {
        System.out.println(name + " is a directory");
      }
    }
  }
}





JarEntry: getCompressedSize()

  
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
  public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
      JarEntry je = (JarEntry) e.nextElement();
      String name = je.getName();
      Date lastModified = new Date(je.getTime());
      long uncompressedSize = je.getSize();
      long compressedSize = je.getCompressedSize();
      System.out.println(lastModified);
      System.out.println(uncompressedSize);
      System.out.println(compressedSize);
    }
  }
}





JarEntry: getCrc()

  
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
  public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
      JarEntry je = (JarEntry) e.nextElement();
      String name = je.getName();
      long crc = je.getCrc();
      System.out.println("Its CRC is " + crc);
      String comment = je.getComment();
      if (comment != null && !comment.equals("")) {
        System.out.println(comment);
      }
      if (je.isDirectory()) {
        System.out.println(name + " is a directory");
      }
    }
  }
}





JarEntry: getName()

  
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
  public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
      JarEntry je = (JarEntry) e.nextElement();
      String name = je.getName();
      System.out.println(name);
    }
  }
}





JarEntry: getSize()

  
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
  public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
      JarEntry je = (JarEntry) e.nextElement();
      String name = je.getName();
      Date lastModified = new Date(je.getTime());
      long uncompressedSize = je.getSize();
      long compressedSize = je.getCompressedSize();
      System.out.println(lastModified);
      System.out.println(uncompressedSize);
      System.out.println(compressedSize);
    }
  }
}





JarEntry: getTime()

  
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
  public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
      JarEntry je = (JarEntry) e.nextElement();
      String name = je.getName();
      Date lastModified = new Date(je.getTime());
      long uncompressedSize = je.getSize();
      long compressedSize = je.getCompressedSize();
      System.out.println(lastModified);
      System.out.println(uncompressedSize);
      System.out.println(compressedSize);
    }
  }
}





JarEntry: isDirectory()

  
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class Main {
  public static void main(String[] args) throws IOException {
    JarFile jf = new JarFile(args[0]);
    Enumeration e = jf.entries();
    while (e.hasMoreElements()) {
      JarEntry je = (JarEntry) e.nextElement();
      String name = je.getName();
      long crc = je.getCrc();
      System.out.println("Its CRC is " + crc);
      String comment = je.getComment();
      if (comment != null && !comment.equals("")) {
        System.out.println(comment);
      }
      if (je.isDirectory()) {
        System.out.println(name + " is a directory");
      }
    }
  }
}





JarEntry: setTime(long time)

 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
public class Main {
  public static int BUFFER_SIZE = 10240;
  void createJarArchive(File archiveFile, File[] tobeJared) throws Exception {
    byte buffer[] = new byte[BUFFER_SIZE];
    FileOutputStream stream = new FileOutputStream(archiveFile);
    JarOutputStream out = new JarOutputStream(stream, new Manifest());
    for (int i = 0; i < tobeJared.length; i++) {
      if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory())
        continue; // Just in case...
      JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
      jarAdd.setTime(tobeJared[i].lastModified());
      out.putNextEntry(jarAdd);
      FileInputStream in = new FileInputStream(tobeJared[i]);
      while (true) {
        int nRead = in.read(buffer, 0, buffer.length);
        if (nRead <= 0)
          break;
        out.write(buffer, 0, nRead);
      }
      in.close();
    }
    out.close();
    stream.close();
  }
}