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

Last change on this file was 12429, checked in by Mark Evenson, 15 years ago

Correct svn:eol-style and svn:keywords.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/*
2 * unzip.java
3 *
4 * Copyright (C) 2010 Mark Evenson
5 * $Id: unzip.java 12429 2010-02-08 07:43:38Z 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// ### unzip pathname directory => unzipped_pathnames
46public final class unzip 
47  extends Primitive
48{
49    public unzip() {
50        super("unzip", PACKAGE_SYS, true, "pathname &optional directory => unzipped_pathnames");
51    }
52 
53    @Override
54    public LispObject execute(LispObject first) {
55        Pathname zipFile = coerceToPathname(first);
56        Pathname directory = coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue());
57        return unzipToDirectory(zipFile, directory);
58    }
59
60    @Override
61    public LispObject execute(LispObject first, LispObject second) {
62        Pathname zipFile = coerceToPathname(first);
63        Pathname directory = coerceToPathname(second);
64        directory.name = NIL;
65        directory.type = NIL;
66        directory.invalidateNamestring();
67        return unzipToDirectory(zipFile, directory);
68    }
69 
70    private LispObject unzipToDirectory(Pathname zipPath, Pathname dirPath) {
71        if (!zipPath.isAbsolute()) {
72            zipPath = Pathname.mergePathnames(zipPath,
73                                              coerceToPathname(Symbol.DEFAULT_PATHNAME_DEFAULTS.symbolValue()));
74        }
75        LispObject o = Pathname.truename(zipPath, false);
76        if (!(o instanceof Pathname)) {
77            return error(new FileError("No file found: " + zipPath, zipPath));
78        }
79        String zip = ((Pathname)o).getNamestring();
80        if (zip == null) {
81            return error(new FileError("Pathname has no namestring: " + zip, zipPath));
82        }
83        String dir = dirPath.getNamestring();
84        if (dir == null) {
85            return error(new FileError("Could not parse diretory: " + dirPath, dirPath));
86        }
87        LispObject result = NIL;
88        try {
89            ZipFile zipfile = new ZipFile(zip);
90           
91            byte[] buffer = new byte[4096];
92            for (Enumeration<? extends ZipEntry> entries =  zipfile.entries();entries.hasMoreElements();) {
93                ZipEntry entry = entries.nextElement();
94                String name = entry.getName();
95                String filename = dir + name;
96                File file = new File(filename);
97                if (entry.isDirectory()) {
98                    file.mkdirs();
99                    continue;
100                }
101                FileOutputStream out = new FileOutputStream(file);
102                InputStream in = zipfile.getInputStream(entry);
103                int n;
104                while ((n = in.read(buffer)) > 0) {
105                    out.write(buffer, 0, n);
106                }
107                out.close();
108                in.close();
109                result = result.push(new Pathname(filename));
110            }
111        } catch (IOException e) {
112            return error(new FileError("Failed to unzip " 
113                                       + "'" + zipPath + "'"
114                                       + " into " + "'" + dirPath + "'"
115                                       + ": " + e, zipPath)); 
116        }
117        return result;
118    }
119
120    private static final Primitive unzip = new unzip();
121}
Note: See TracBrowser for help on using the repository browser.