| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.store.fs.zip; |
| 7 |
|
| 8 |
import java.io.FileNotFoundException; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.OutputStream; |
| 11 |
import java.nio.channels.FileChannel; |
| 12 |
import java.util.ArrayList; |
| 13 |
import java.util.Enumeration; |
| 14 |
import java.util.zip.ZipEntry; |
| 15 |
import java.util.zip.ZipFile; |
| 16 |
import org.h2.message.DbException; |
| 17 |
import org.h2.store.fs.FilePath; |
| 18 |
import org.h2.store.fs.disk.FilePathDisk; |
| 19 |
|
| 20 |
/** |
| 21 |
* This is a read-only file system that allows |
| 22 |
* to access databases stored in a .zip or .jar file. |
| 23 |
*/ |
| 24 |
public class FilePathZip extends FilePath { |
| 25 |
|
| 26 |
@Override |
| 27 |
public FilePathZip getPath(String path) { |
| 28 |
FilePathZip p = new FilePathZip(); |
| 29 |
p.name = path; |
| 30 |
return p; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public void createDirectory() { |
| 35 |
// ignore |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public boolean createFile() { |
| 40 |
throw DbException.getUnsupportedException("write"); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void delete() { |
| 45 |
throw DbException.getUnsupportedException("write"); |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public boolean exists() { |
| 50 |
try { |
| 51 |
String entryName = getEntryName(); |
| 52 |
if (entryName.isEmpty()) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
try (ZipFile file = openZipFile()) { |
| 56 |
return file.getEntry(entryName) != null; |
| 57 |
} |
| 58 |
} catch (IOException e) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public long lastModified() { |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public FilePath getParent() { |
| 70 |
int idx = name.lastIndexOf('/'); |
| 71 |
return idx < 0 ? null : getPath(name.substring(0, idx)); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public boolean isAbsolute() { |
| 76 |
String fileName = translateFileName(name); |
| 77 |
return FilePath.get(fileName).isAbsolute(); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public FilePath unwrap() { |
| 82 |
return FilePath.get(name.substring(getScheme().length() + 1)); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public boolean isDirectory() { |
| 87 |
return isRegularOrDirectory(true); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public boolean isRegularFile() { |
| 92 |
return isRegularOrDirectory(false); |
| 93 |
} |
| 94 |
|
| 95 |
private boolean isRegularOrDirectory(boolean directory) { |
| 96 |
try { |
| 97 |
String entryName = getEntryName(); |
| 98 |
if (entryName.isEmpty()) { |
| 99 |
return directory; |
| 100 |
} |
| 101 |
try (ZipFile file = openZipFile()) { |
| 102 |
Enumeration<? extends ZipEntry> en = file.entries(); |
| 103 |
while (en.hasMoreElements()) { |
| 104 |
ZipEntry entry = en.nextElement(); |
| 105 |
String n = entry.getName(); |
| 106 |
if (n.equals(entryName)) { |
| 107 |
return entry.isDirectory() == directory; |
| 108 |
} else if (n.startsWith(entryName)) { |
| 109 |
if (n.length() == entryName.length() + 1) { |
| 110 |
if (n.equals(entryName + "/")) { |
| 111 |
return directory; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
return false; |
| 118 |
} catch (IOException e) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public boolean canWrite() { |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public boolean setReadOnly() { |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public long size() { |
| 135 |
try { |
| 136 |
try (ZipFile file = openZipFile()) { |
| 137 |
ZipEntry entry = file.getEntry(getEntryName()); |
| 138 |
return entry == null ? 0 : entry.getSize(); |
| 139 |
} |
| 140 |
} catch (IOException e) { |
| 141 |
return 0; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public ArrayList<FilePath> newDirectoryStream() { |
| 147 |
String path = name; |
| 148 |
ArrayList<FilePath> list = new ArrayList<>(); |
| 149 |
try { |
| 150 |
if (path.indexOf('!') < 0) { |
| 151 |
path += "!"; |
| 152 |
} |
| 153 |
if (!path.endsWith("/")) { |
| 154 |
path += "/"; |
| 155 |
} |
| 156 |
try (ZipFile file = openZipFile()) { |
| 157 |
String dirName = getEntryName(); |
| 158 |
String prefix = path.substring(0, path.length() - dirName.length()); |
| 159 |
Enumeration<? extends ZipEntry> en = file.entries(); |
| 160 |
while (en.hasMoreElements()) { |
| 161 |
ZipEntry entry = en.nextElement(); |
| 162 |
String name = entry.getName(); |
| 163 |
if (!name.startsWith(dirName)) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
if (name.length() <= dirName.length()) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
int idx = name.indexOf('/', dirName.length()); |
| 170 |
if (idx < 0 || idx >= name.length() - 1) { |
| 171 |
list.add(getPath(prefix + name)); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
return list; |
| 176 |
} catch (IOException e) { |
| 177 |
throw DbException.convertIOException(e, "listFiles " + path); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public FileChannel open(String mode) throws IOException { |
| 183 |
ZipFile file = openZipFile(); |
| 184 |
ZipEntry entry = file.getEntry(getEntryName()); |
| 185 |
if (entry == null) { |
| 186 |
file.close(); |
| 187 |
throw new FileNotFoundException(name); |
| 188 |
} |
| 189 |
return new FileZip(file, entry); |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public OutputStream newOutputStream(boolean append) throws IOException { |
| 194 |
throw new IOException("write"); |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public void moveTo(FilePath newName, boolean atomicReplace) { |
| 199 |
throw DbException.getUnsupportedException("write"); |
| 200 |
} |
| 201 |
|
| 202 |
private static String translateFileName(String fileName) { |
| 203 |
if (fileName.startsWith("zip:")) { |
| 204 |
fileName = fileName.substring("zip:".length()); |
| 205 |
} |
| 206 |
int idx = fileName.indexOf('!'); |
| 207 |
if (idx >= 0) { |
| 208 |
fileName = fileName.substring(0, idx); |
| 209 |
} |
| 210 |
return FilePathDisk.expandUserHomeDirectory(fileName); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public FilePath toRealPath() { |
| 215 |
return this; |
| 216 |
} |
| 217 |
|
| 218 |
private String getEntryName() { |
| 219 |
int idx = name.indexOf('!'); |
| 220 |
String fileName; |
| 221 |
if (idx <= 0) { |
| 222 |
fileName = ""; |
| 223 |
} else { |
| 224 |
fileName = name.substring(idx + 1); |
| 225 |
} |
| 226 |
fileName = fileName.replace('\\', '/'); |
| 227 |
if (fileName.startsWith("/")) { |
| 228 |
fileName = fileName.substring(1); |
| 229 |
} |
| 230 |
return fileName; |
| 231 |
} |
| 232 |
|
| 233 |
private ZipFile openZipFile() throws IOException { |
| 234 |
String fileName = translateFileName(name); |
| 235 |
return new ZipFile(fileName); |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public FilePath createTempFile(String suffix, boolean inTempDir) throws IOException { |
| 240 |
if (!inTempDir) { |
| 241 |
throw new IOException("File system is read-only"); |
| 242 |
} |
| 243 |
return new FilePathDisk().getPath(name).createTempFile(suffix, true); |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public String getScheme() { |
| 248 |
return "zip"; |
| 249 |
} |
| 250 |
|
| 251 |
} |