source: branches/0.25.x/abcl/src/org/armedbear/lisp/zip.java

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

Improve docstring for SYS:ZIP.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/*
2 * zip.java
3 *
4 * Copyright (C) 2005 Peter Graves
5 * $Id: zip.java 13016 2010-11-10 21:13:42Z 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.*;
37
38import java.io.File;
39import java.io.FileInputStream;
40import java.io.FileOutputStream;
41import java.io.IOException;
42import java.util.HashSet;
43import java.util.Set;
44import java.util.zip.ZipEntry;
45import java.util.zip.ZipOutputStream;
46
47@DocString(name="zip",
48           args="pathname pathnames &optional topdir",
49           doc="Creates a zip archive at PATHNAME whose entries enumerated via the list of PATHNAMES.\n"
50           + "If the optional TOPDIR argument is specified, the archive will "
51           + "preserve the hierarchy of PATHNAMES relative to TOPDIR.  Without "
52           + "TOPDIR, there will be no sub-directories in the archive, i.e. it will "
53           + "be flat.")
54public final class zip extends Primitive
55{
56    private zip()
57    {
58        super("zip", PACKAGE_SYS, true);
59    }
60
61    @Override
62    public LispObject execute(LispObject first, LispObject second)
63    {
64        Pathname zipfilePathname = coerceToPathname(first);
65        byte[] buffer = new byte[4096];
66        try {
67            String zipfileNamestring = zipfilePathname.getNamestring();
68            if (zipfileNamestring == null)
69                return error(new SimpleError("Pathname has no namestring: " +
70                                              zipfilePathname.writeToString()));
71            ZipOutputStream out =
72                new ZipOutputStream(new FileOutputStream(zipfileNamestring));
73            LispObject list = second;
74            while (list != NIL) {
75                Pathname pathname = coerceToPathname(list.car());
76                String namestring = pathname.getNamestring();
77                if (namestring == null) {
78                    // Clean up before signalling error.
79                    out.close();
80                    File zipfile = new File(zipfileNamestring);
81                    zipfile.delete();
82                    return error(new SimpleError("Pathname has no namestring: " +
83                                                  pathname.writeToString()));
84                }
85                File file = new File(namestring);
86                FileInputStream in = new FileInputStream(file);
87                ZipEntry entry = new ZipEntry(file.getName());
88                out.putNextEntry(entry);
89                int n;
90                while ((n = in.read(buffer)) > 0)
91                    out.write(buffer, 0, n);
92                out.closeEntry();
93                in.close();
94                list = list.cdr();
95            }
96            out.close();
97        }
98        catch (IOException e) {
99            return error(new LispError(e.getMessage()));
100        }
101        return zipfilePathname;
102    }
103
104    @Override
105    public LispObject execute(LispObject first, LispObject second, LispObject third)
106    {
107        Pathname zipfilePathname = coerceToPathname(first);
108        byte[] buffer = new byte[4096];
109        try {
110            String zipfileNamestring = zipfilePathname.getNamestring();
111            if (zipfileNamestring == null)
112                return error(new SimpleError("Pathname has no namestring: " +
113                                              zipfilePathname.writeToString()));
114            ZipOutputStream out =
115                new ZipOutputStream(new FileOutputStream(zipfileNamestring));
116            Pathname root = (Pathname)coerceToPathname(third);
117            String rootPath = root.getDirectoryNamestring();
118            int rootPathLength = rootPath.length();
119            Set<String> directories = new HashSet<String>();
120            LispObject list = second;
121            while (list != NIL) {
122                Pathname pathname = coerceToPathname(list.car());
123                String namestring = pathname.getNamestring();
124                if (namestring == null) {
125                    // Clean up before signalling error.
126                    out.close();
127                    File zipfile = new File(zipfileNamestring);
128                    zipfile.delete();
129                    return error(new SimpleError("Pathname has no namestring: " +
130                                                  pathname.writeToString()));
131                }
132                String directory = "";
133                String dir = pathname.getDirectoryNamestring();
134                if (dir.length() > rootPathLength) {
135                  String d = dir.substring(rootPathLength);
136                  int i = 0;
137                  int j;
138                  while ((j = d.indexOf(Pathname.separator, i)) != -1) {
139                    i = j + 1;
140                    directory = d.substring(0, j) + Pathname.separator;
141                    if (!directories.contains(directory)) {
142                      directories.add(directory);
143                      ZipEntry entry = new ZipEntry(directory);
144                      out.putNextEntry(entry);
145                      out.closeEntry();
146                    }
147                  }
148                }
149                File file = new File(namestring);
150                if (file.isDirectory()) {
151                    list = list.cdr();
152                    continue;
153                }
154                FileInputStream in = new FileInputStream(file);
155                ZipEntry entry = new ZipEntry(directory + file.getName());
156                out.putNextEntry(entry);
157                int n;
158                while ((n = in.read(buffer)) > 0)
159                    out.write(buffer, 0, n);
160                out.closeEntry();
161                in.close();
162                list = list.cdr();
163            }
164            out.close();
165        }
166        catch (IOException e) {
167            return error(new LispError(e.getMessage()));
168        }
169        return zipfilePathname;
170    }
171
172
173    private static final Primitive zip = new zip();
174}
Note: See TracBrowser for help on using the repository browser.