Java by API/java.util.jar/JarEntry
Содержание
JarEntry: getAttributes()
<source lang="java">
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]); } } } }
}
</source>
JarEntry: getComment()
<source lang="java">
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"); } } }
}
</source>
JarEntry: getCompressedSize()
<source lang="java">
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); } }
}
</source>
JarEntry: getCrc()
<source lang="java">
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"); } } }
}
</source>
JarEntry: getName()
<source lang="java">
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); } }
}
</source>
JarEntry: getSize()
<source lang="java">
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); } }
}
</source>
JarEntry: getTime()
<source lang="java">
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); } }
}
</source>
JarEntry: isDirectory()
<source lang="java">
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"); } } }
}
</source>
JarEntry: setTime(long time)
<source lang="java">
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(); }
}
</source>