1 | /* |
---|
2 | * zip.java |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Peter Graves |
---|
5 | * $Id: zip.java 12254 2009-11-06 20:07:54Z ehuelsmann $ |
---|
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 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import java.io.File; |
---|
37 | import java.io.FileInputStream; |
---|
38 | import java.io.FileOutputStream; |
---|
39 | import java.io.IOException; |
---|
40 | import java.util.zip.ZipEntry; |
---|
41 | import java.util.zip.ZipOutputStream; |
---|
42 | |
---|
43 | // ### zip pathname pathnames |
---|
44 | public final class zip extends Primitive |
---|
45 | { |
---|
46 | private zip() |
---|
47 | { |
---|
48 | super("zip", PACKAGE_SYS, true, "pathname pathnames"); |
---|
49 | } |
---|
50 | |
---|
51 | @Override |
---|
52 | public LispObject execute(LispObject first, LispObject second) |
---|
53 | |
---|
54 | { |
---|
55 | Pathname zipfilePathname = coerceToPathname(first); |
---|
56 | byte[] buffer = new byte[4096]; |
---|
57 | try { |
---|
58 | String zipfileNamestring = zipfilePathname.getNamestring(); |
---|
59 | if (zipfileNamestring == null) |
---|
60 | return error(new SimpleError("Pathname has no namestring: " + |
---|
61 | zipfilePathname.writeToString())); |
---|
62 | ZipOutputStream out = |
---|
63 | new ZipOutputStream(new FileOutputStream(zipfileNamestring)); |
---|
64 | LispObject list = second; |
---|
65 | while (list != NIL) { |
---|
66 | Pathname pathname = coerceToPathname(list.car()); |
---|
67 | String namestring = pathname.getNamestring(); |
---|
68 | if (namestring == null) { |
---|
69 | // Clean up before signalling error. |
---|
70 | out.close(); |
---|
71 | File zipfile = new File(zipfileNamestring); |
---|
72 | zipfile.delete(); |
---|
73 | return error(new SimpleError("Pathname has no namestring: " + |
---|
74 | pathname.writeToString())); |
---|
75 | } |
---|
76 | File file = new File(namestring); |
---|
77 | FileInputStream in = new FileInputStream(file); |
---|
78 | ZipEntry entry = new ZipEntry(file.getName()); |
---|
79 | out.putNextEntry(entry); |
---|
80 | int n; |
---|
81 | while ((n = in.read(buffer)) > 0) |
---|
82 | out.write(buffer, 0, n); |
---|
83 | out.closeEntry(); |
---|
84 | in.close(); |
---|
85 | list = list.cdr(); |
---|
86 | } |
---|
87 | out.close(); |
---|
88 | } |
---|
89 | catch (IOException e) { |
---|
90 | return error(new LispError(e.getMessage())); |
---|
91 | } |
---|
92 | return zipfilePathname; |
---|
93 | } |
---|
94 | |
---|
95 | private static final Primitive zip = new zip(); |
---|
96 | } |
---|