source: branches/streams/abcl/src/org/armedbear/lisp/unzip.java

Last change on this file was 13944, checked in by Mark Evenson, 13 years ago

Add docstring for SYS:UNZIP.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1/*
2 * unzip.java
3 *
4 * Copyright (C) 2010 Mark Evenson
5 * $Id: unzip.java 13944 2012-05-24 11:11:43Z mevenson $
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37import java.io.File;
38import java.io.InputStream;
39import java.io.FileOutputStream;
40import java.io.IOException;
41import java.util.Enumeration;
42import java.util.zip.ZipEntry;
43import java.util.zip.ZipFile;
44
45@DocString(name="unzip",
46           args="pathname &optional directory => unzipped_pathnames",
47           doc="Unpack zip archive at PATHNAME returning a list of extracted pathnames.\nIf the optional DIRECTORY is specified, root the abstraction in that directory, otherwise use the current value of *DEFAULT-PATHNAME-DEFAULTS.")
48public final class unzip 
49  extends Primitive
50{
51    public unzip() {
52        super("unzip", PACKAGE_SYS, true, 
53              "pathname &optional directory => unzipped_pathnames");
54    }
55 
56    @Override
57    public LispObject execute(LispObject first) {
58        Pathname zipFile = coerceToPathname(first);
59        Pathname directory = coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue());
60        return unzipToDirectory(zipFile, directory);
61    }
62
63    @Override
64    public LispObject execute(LispObject first, LispObject second) {
65        Pathname zipFile = coerceToPathname(first);
66        Pathname directory = coerceToPathname(second);
67        directory.name = NIL;
68        directory.type = NIL;
69        directory.invalidateNamestring();
70        return unzipToDirectory(zipFile, directory);
71    }
72 
73    private LispObject unzipToDirectory(Pathname zipPath, Pathname dirPath) {
74        if (!zipPath.isAbsolute()) {
75            zipPath = Pathname.mergePathnames(zipPath,
76                                              coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue()));
77        }
78        LispObject o = Pathname.truename(zipPath, false);
79        if (!(o instanceof Pathname)) {
80            return error(new FileError("No file found: " + zipPath, zipPath));
81        }
82        String zip = ((Pathname)o).getNamestring();
83        if (zip == null) {
84            return error(new FileError("Pathname has no namestring: " + zip, zipPath));
85        }
86        String dir = dirPath.getNamestring();
87        if (dir == null) {
88            return error(new FileError("Could not parse diretory: " + dirPath, dirPath));
89        }
90        LispObject result = NIL;
91        try {
92            ZipFile zipfile = new ZipFile(zip);
93           
94            byte[] buffer = new byte[4096];
95            for (Enumeration<? extends ZipEntry> entries =  zipfile.entries();entries.hasMoreElements();) {
96                ZipEntry entry = entries.nextElement();
97                String name = entry.getName();
98                String filename = dir + name;
99                File file = new File(filename);
100                if (entry.isDirectory()) {
101                    file.mkdirs();
102                    continue;
103                }
104                FileOutputStream out = new FileOutputStream(file);
105                InputStream in = zipfile.getInputStream(entry);
106                int n;
107                while ((n = in.read(buffer)) > 0) {
108                    out.write(buffer, 0, n);
109                }
110                out.close();
111                in.close();
112                result = result.push(new Pathname(filename));
113            }
114        } catch (IOException e) {
115            return error(new FileError("Failed to unzip " 
116                                       + "'" + zipPath + "'"
117                                       + " into " + "'" + dirPath + "'"
118                                       + ": " + e, zipPath)); 
119        }
120        return result;
121    }
122
123    private static final Primitive unzip = new unzip();
124}
Note: See TracBrowser for help on using the repository browser.