Source Code Cross Referenced for ZipArchive.java in  » Scripting » Kawa » gnu » bytecode » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » Kawa » gnu.bytecode 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.