KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > ZipArchive


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5 import java.io.*;
6 import java.util.zip.*;
7
8 /** A class to manipulate a .zip archive.
9  * Does not handle compression/uncompression, though that could be added.
10  * When used an an application. provides a simplified tar-like interface.
11  * @author Per Bothner <bothner@cygnus.com>
12  */

13
14 public class ZipArchive
15 {
16   private static void usage ()
17   {
18     System.err.println ("zipfile [ptxq] archive [file ...]");
19     System.exit (-1);
20   }
21
22   public static long copy(InputStream in, OutputStream out, byte[] buffer)
23     throws IOException
24   {
25     long total = 0;
26     for (;;)
27       {
28     int count = in.read(buffer);
29     if (count <= 0)
30       return total;
31     out.write(buffer, 0, count);
32     total += count;
33       }
34   }
35
36   public static void copy(InputStream in, String JavaDoc name, byte[] buffer)
37     throws IOException
38     {
39     File f = new File(name);
40     String JavaDoc dir_name = f.getParent();
41     if (dir_name != null)
42       {
43     File dir = new File(dir_name);
44     if (! dir.exists())
45       System.err.println("mkdirs:"+dir.mkdirs());
46       }
47     if (name.charAt (name.length () - 1) != '/')
48       {
49     OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
50     copy(in, out, buffer);
51     out.close ();
52       }
53   }
54
55   /**
56    * Manipulate a .zip archive using a tar-like interface.
57    * <p>
58    * Usage: <code>ZipArchive</code> <var>command archive</var> [<var>file</var> ...]
59    * <dl>
60    * <dt><code>ZipArchive t</code> <var>archive file</var> ...<dd>
61    * List information about the named members of the archive.
62    * <dt><code>ZipArchive x</code> <var>archive file</var> ...<dd>
63    * Extract the named members from the archive.
64    * <dt><code>ZipArchive p</code> <var>archive file</var> ...<dd>
65    * Print the named members from the archive on standard output.
66    * Prints just the raw contents, with no headers or conversion.
67    * <dt><code>ZipArchive</code> [<code>ptx</code>] <var>archive</var><dd>
68    * With no arguments, does each command for every member in the archive.
69    * <dt><code>ZipArchive q</code> <var>archive file</var> ...<dd>
70    * Add the named files to the end of archive.
71    * Does not check for duplicates.
72    * </dl>
73    */

74
75   public static void main (String JavaDoc args[]) throws IOException
76   {
77     if (args.length < 2)
78       usage ();
79     String JavaDoc command = args[0];
80     String JavaDoc archive_name = args[1];
81
82     try
83       {
84     if (command.equals ("t")
85         || command.equals ("p")
86         || command.equals ("x"))
87       {
88         PrintStream out = System.out;
89         byte[] buf = new byte[1024];
90         if (args.length == 2)
91           {
92         BufferedInputStream in
93           = new BufferedInputStream(new FileInputStream(archive_name));
94         ZipInputStream zin = new ZipInputStream (in);
95         ZipEntry zent;
96         while ((zent = zin.getNextEntry()) != null)
97           {
98             String JavaDoc name = zent.getName();
99             if (command.equals("t"))
100               {
101             out.print(name);
102             out.print(" size: ");
103             out.println(zent.getSize());
104               }
105             else if (command.equals("p"))
106               {
107             copy(zin, out, buf);
108               }
109             else // commend.equals("x")
110
{
111             copy(zin, name, buf);
112               }
113           }
114           }
115         else
116           {
117         ZipFile zar = new ZipFile(archive_name);
118         for (int i = 2; i < args.length; i++)
119           {
120             String JavaDoc name = args[i];
121             ZipEntry zent = zar.getEntry(name);
122             if (zent == null)
123               {
124             System.err.println ("zipfile " + archive_name + ":" +
125                         args[i] + " - not found");
126             System.exit (-1);
127               }
128             else if (command.equals("t"))
129               {
130             out.print(name);
131             out.print(" size: ");
132             out.println(zent.getSize());
133               }
134             else if (command.equals("p"))
135               {
136             copy(zar.getInputStream(zent), out, buf);
137               }
138             else // commend.equals("x")
139
{
140             copy(zar.getInputStream(zent), name, buf);
141               }
142           }
143           }
144       }
145     else if (command.equals ("q"))
146       {
147         ZipOutputStream zar
148           = new ZipOutputStream(new FileOutputStream(archive_name));
149         for (int i = 2; i < args.length; i++)
150           {
151         File in = new File (args[i]);
152         if (!in.exists ())
153           throw new IOException (args[i] + " - not found");
154         if (!in.canRead ())
155           throw new IOException (args[i] + " - not readable");
156         int size = (int) in.length ();
157         FileInputStream fin = new FileInputStream (in);
158         byte[] contents = new byte[size];
159         if (fin.read (contents) != size)
160           throw new IOException (args[i] + " - read error");
161         fin.close ();
162
163         ZipEntry ze = new ZipEntry(args[i]);
164         ze.setSize(size);
165         ze.setTime(in.lastModified());
166         zar.putNextEntry(ze);
167         zar.write(contents, 0, size);
168           }
169         zar.close ();
170       }
171     else
172       usage ();
173       }
174     catch (IOException ex)
175       {
176     System.err.println ("I/O Exception: " + ex);
177       }
178   }
179 }
180
Popular Tags