001: // Copyright (c) 1997 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.bytecode;
005:
006: import java.io.*;
007: import java.util.zip.*;
008:
009: /** A class to manipulate a .zip archive.
010: * Does not handle compression/uncompression, though that could be added.
011: * When used an an application. provides a simplified tar-like interface.
012: * @author Per Bothner <bothner@cygnus.com>
013: */
014:
015: public class ZipArchive {
016: private static void usage() {
017: System.err.println("zipfile [ptxq] archive [file ...]");
018: System.exit(-1);
019: }
020:
021: public static long copy(InputStream in, OutputStream out,
022: byte[] buffer) throws IOException {
023: long total = 0;
024: for (;;) {
025: int count = in.read(buffer);
026: if (count <= 0)
027: return total;
028: out.write(buffer, 0, count);
029: total += count;
030: }
031: }
032:
033: public static void copy(InputStream in, String name, byte[] buffer)
034: throws IOException {
035: File f = new File(name);
036: String dir_name = f.getParent();
037: if (dir_name != null) {
038: File dir = new File(dir_name);
039: if (!dir.exists())
040: System.err.println("mkdirs:" + dir.mkdirs());
041: }
042: if (name.charAt(name.length() - 1) != '/') {
043: OutputStream out = new BufferedOutputStream(
044: new FileOutputStream(f));
045: copy(in, out, buffer);
046: out.close();
047: }
048: }
049:
050: /**
051: * Manipulate a .zip archive using a tar-like interface.
052: * <p>
053: * Usage: <code>ZipArchive</code> <var>command archive</var> [<var>file</var> ...]
054: * <dl>
055: * <dt><code>ZipArchive t</code> <var>archive file</var> ...<dd>
056: * List information about the named members of the archive.
057: * <dt><code>ZipArchive x</code> <var>archive file</var> ...<dd>
058: * Extract the named members from the archive.
059: * <dt><code>ZipArchive p</code> <var>archive file</var> ...<dd>
060: * Print the named members from the archive on standard output.
061: * Prints just the raw contents, with no headers or conversion.
062: * <dt><code>ZipArchive</code> [<code>ptx</code>] <var>archive</var><dd>
063: * With no arguments, does each command for every member in the archive.
064: * <dt><code>ZipArchive q</code> <var>archive file</var> ...<dd>
065: * Add the named files to the end of archive.
066: * Does not check for duplicates.
067: * </dl>
068: */
069:
070: public static void main(String args[]) throws IOException {
071: if (args.length < 2)
072: usage();
073: String command = args[0];
074: String archive_name = args[1];
075:
076: try {
077: if (command.equals("t") || command.equals("p")
078: || command.equals("x")) {
079: PrintStream out = System.out;
080: byte[] buf = new byte[1024];
081: if (args.length == 2) {
082: BufferedInputStream in = new BufferedInputStream(
083: new FileInputStream(archive_name));
084: ZipInputStream zin = new ZipInputStream(in);
085: ZipEntry zent;
086: while ((zent = zin.getNextEntry()) != null) {
087: String name = zent.getName();
088: if (command.equals("t")) {
089: out.print(name);
090: out.print(" size: ");
091: out.println(zent.getSize());
092: } else if (command.equals("p")) {
093: copy(zin, out, buf);
094: } else // commend.equals("x")
095: {
096: copy(zin, name, buf);
097: }
098: }
099: } else {
100: ZipFile zar = new ZipFile(archive_name);
101: for (int i = 2; i < args.length; i++) {
102: String name = args[i];
103: ZipEntry zent = zar.getEntry(name);
104: if (zent == null) {
105: System.err.println("zipfile "
106: + archive_name + ":" + args[i]
107: + " - not found");
108: System.exit(-1);
109: } else if (command.equals("t")) {
110: out.print(name);
111: out.print(" size: ");
112: out.println(zent.getSize());
113: } else if (command.equals("p")) {
114: copy(zar.getInputStream(zent), out, buf);
115: } else // commend.equals("x")
116: {
117: copy(zar.getInputStream(zent), name, buf);
118: }
119: }
120: }
121: } else if (command.equals("q")) {
122: ZipOutputStream zar = new ZipOutputStream(
123: new FileOutputStream(archive_name));
124: for (int i = 2; i < args.length; i++) {
125: File in = new File(args[i]);
126: if (!in.exists())
127: throw new IOException(args[i] + " - not found");
128: if (!in.canRead())
129: throw new IOException(args[i]
130: + " - not readable");
131: int size = (int) in.length();
132: FileInputStream fin = new FileInputStream(in);
133: byte[] contents = new byte[size];
134: if (fin.read(contents) != size)
135: throw new IOException(args[i] + " - read error");
136: fin.close();
137:
138: ZipEntry ze = new ZipEntry(args[i]);
139: ze.setSize(size);
140: ze.setTime(in.lastModified());
141: zar.putNextEntry(ze);
142: zar.write(contents, 0, size);
143: }
144: zar.close();
145: } else
146: usage();
147: } catch (IOException ex) {
148: System.err.println("I/O Exception: " + ex);
149: }
150: }
151: }
|