| 1 | /* |
|---|
| 2 | * zip.java |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2005 Peter Graves |
|---|
| 5 | * $Id: zip.java 12402 2010-01-26 11:15:48Z 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 | |
|---|
| 34 | package org.armedbear.lisp; |
|---|
| 35 | |
|---|
| 36 | import static org.armedbear.lisp.Lisp.*; |
|---|
| 37 | |
|---|
| 38 | import java.io.File; |
|---|
| 39 | import java.io.FileInputStream; |
|---|
| 40 | import java.io.FileOutputStream; |
|---|
| 41 | import java.io.IOException; |
|---|
| 42 | import java.util.HashSet; |
|---|
| 43 | import java.util.Set; |
|---|
| 44 | import java.util.zip.ZipEntry; |
|---|
| 45 | import java.util.zip.ZipOutputStream; |
|---|
| 46 | |
|---|
| 47 | // ### zip pathname pathnames |
|---|
| 48 | public final class zip extends Primitive |
|---|
| 49 | { |
|---|
| 50 | private zip() |
|---|
| 51 | { |
|---|
| 52 | super("zip", PACKAGE_SYS, true, "pathname pathnames &optional topdir"); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | @Override |
|---|
| 56 | public LispObject execute(LispObject first, LispObject second) |
|---|
| 57 | |
|---|
| 58 | { |
|---|
| 59 | Pathname zipfilePathname = coerceToPathname(first); |
|---|
| 60 | byte[] buffer = new byte[4096]; |
|---|
| 61 | try { |
|---|
| 62 | String zipfileNamestring = zipfilePathname.getNamestring(); |
|---|
| 63 | if (zipfileNamestring == null) |
|---|
| 64 | return error(new SimpleError("Pathname has no namestring: " + |
|---|
| 65 | zipfilePathname.writeToString())); |
|---|
| 66 | ZipOutputStream out = |
|---|
| 67 | new ZipOutputStream(new FileOutputStream(zipfileNamestring)); |
|---|
| 68 | LispObject list = second; |
|---|
| 69 | while (list != NIL) { |
|---|
| 70 | Pathname pathname = coerceToPathname(list.car()); |
|---|
| 71 | String namestring = pathname.getNamestring(); |
|---|
| 72 | if (namestring == null) { |
|---|
| 73 | // Clean up before signalling error. |
|---|
| 74 | out.close(); |
|---|
| 75 | File zipfile = new File(zipfileNamestring); |
|---|
| 76 | zipfile.delete(); |
|---|
| 77 | return error(new SimpleError("Pathname has no namestring: " + |
|---|
| 78 | pathname.writeToString())); |
|---|
| 79 | } |
|---|
| 80 | File file = new File(namestring); |
|---|
| 81 | FileInputStream in = new FileInputStream(file); |
|---|
| 82 | ZipEntry entry = new ZipEntry(file.getName()); |
|---|
| 83 | out.putNextEntry(entry); |
|---|
| 84 | int n; |
|---|
| 85 | while ((n = in.read(buffer)) > 0) |
|---|
| 86 | out.write(buffer, 0, n); |
|---|
| 87 | out.closeEntry(); |
|---|
| 88 | in.close(); |
|---|
| 89 | list = list.cdr(); |
|---|
| 90 | } |
|---|
| 91 | out.close(); |
|---|
| 92 | } |
|---|
| 93 | catch (IOException e) { |
|---|
| 94 | return error(new LispError(e.getMessage())); |
|---|
| 95 | } |
|---|
| 96 | return zipfilePathname; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | @Override |
|---|
| 100 | public LispObject execute(LispObject first, LispObject second, LispObject third) |
|---|
| 101 | { |
|---|
| 102 | Pathname zipfilePathname = coerceToPathname(first); |
|---|
| 103 | byte[] buffer = new byte[4096]; |
|---|
| 104 | try { |
|---|
| 105 | String zipfileNamestring = zipfilePathname.getNamestring(); |
|---|
| 106 | if (zipfileNamestring == null) |
|---|
| 107 | return error(new SimpleError("Pathname has no namestring: " + |
|---|
| 108 | zipfilePathname.writeToString())); |
|---|
| 109 | ZipOutputStream out = |
|---|
| 110 | new ZipOutputStream(new FileOutputStream(zipfileNamestring)); |
|---|
| 111 | Pathname root = (Pathname)coerceToPathname(third); |
|---|
| 112 | String rootPath = root.getDirectoryNamestring(); |
|---|
| 113 | int rootPathLength = rootPath.length(); |
|---|
| 114 | Set<String> directories = new HashSet<String>(); |
|---|
| 115 | LispObject list = second; |
|---|
| 116 | while (list != NIL) { |
|---|
| 117 | Pathname pathname = coerceToPathname(list.car()); |
|---|
| 118 | String namestring = pathname.getNamestring(); |
|---|
| 119 | if (namestring == null) { |
|---|
| 120 | // Clean up before signalling error. |
|---|
| 121 | out.close(); |
|---|
| 122 | File zipfile = new File(zipfileNamestring); |
|---|
| 123 | zipfile.delete(); |
|---|
| 124 | return error(new SimpleError("Pathname has no namestring: " + |
|---|
| 125 | pathname.writeToString())); |
|---|
| 126 | } |
|---|
| 127 | String directory = ""; |
|---|
| 128 | String dir = pathname.getDirectoryNamestring(); |
|---|
| 129 | if (dir.length() > rootPathLength) { |
|---|
| 130 | String d = dir.substring(rootPathLength); |
|---|
| 131 | int i = 0; |
|---|
| 132 | int j; |
|---|
| 133 | while ((j = d.indexOf(File.separator, i)) != -1) { |
|---|
| 134 | i = j + 1; |
|---|
| 135 | directory = d.substring(0, j).replace(File.separatorChar, '/') + "/"; |
|---|
| 136 | if (!directories.contains(directory)) { |
|---|
| 137 | directories.add(directory); |
|---|
| 138 | ZipEntry entry = new ZipEntry(directory); |
|---|
| 139 | out.putNextEntry(entry); |
|---|
| 140 | out.closeEntry(); |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | File file = new File(namestring); |
|---|
| 145 | if (file.isDirectory()) { |
|---|
| 146 | list = list.cdr(); |
|---|
| 147 | continue; |
|---|
| 148 | } |
|---|
| 149 | FileInputStream in = new FileInputStream(file); |
|---|
| 150 | ZipEntry entry = new ZipEntry(directory + file.getName()); |
|---|
| 151 | out.putNextEntry(entry); |
|---|
| 152 | int n; |
|---|
| 153 | while ((n = in.read(buffer)) > 0) |
|---|
| 154 | out.write(buffer, 0, n); |
|---|
| 155 | out.closeEntry(); |
|---|
| 156 | in.close(); |
|---|
| 157 | list = list.cdr(); |
|---|
| 158 | } |
|---|
| 159 | out.close(); |
|---|
| 160 | } |
|---|
| 161 | catch (IOException e) { |
|---|
| 162 | return error(new LispError(e.getMessage())); |
|---|
| 163 | } |
|---|
| 164 | return zipfilePathname; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | private static final Primitive zip = new zip(); |
|---|
| 169 | } |
|---|