下面列出了怎么用java.util.zip.ZipError的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
public static void unzip(Path zipFile, Path targetDir) throws IOException {
try {
if (!Files.exists(targetDir)) {
Files.createDirectories(targetDir);
}
} catch (FileAlreadyExistsException fae) {
throw new IOException("Could not create directory '" + targetDir + "' as a file already exists with the same name");
}
try (FileSystem zipfs = newFileSystem(zipFile)) {
for (Path zipRoot : zipfs.getRootDirectories()) {
copyFromZip(zipRoot, targetDir);
}
} catch (IOException | ZipError ioe) {
// TODO: (at a later date) Get rid of the ZipError catching (and instead only catch IOException)
// since it's a JDK bug which threw the undeclared ZipError instead of an IOException.
// Java 9 fixes it https://bugs.openjdk.java.net/browse/JDK-8062754
throw new IOException("Could not unzip " + zipFile + " to target dir " + targetDir, ioe);
}
}
/**
* This call is not thread safe, a single of FileSystem can be created for the
* profided uri until it is closed.
*
* @param uri The uri to the zip file.
* @param env Env map.
* @return A new FileSystem.
* @throws IOException in case of a failure
*/
public static FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException {
// If Multi threading required, logic should be added to wrap this fs
// onto a fs that handles a reference counter and close the fs only when all thread are done
// with it.
try {
return FileSystems.newFileSystem(uri, env);
} catch (IOException | ZipError ioe) {
// TODO: (at a later date) Get rid of the ZipError catching (and instead only catch IOException)
// since it's a JDK bug which threw the undeclared ZipError instead of an IOException.
// Java 9 fixes it https://bugs.openjdk.java.net/browse/JDK-8062754
// include the URI for which the filesystem creation failed
throw new IOException("Failed to create a new filesystem for " + uri, ioe);
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
@Override
public FileSystem newFileSystem(URI uri, Map<String, ?> env)
throws IOException
{
Path path = uriToPath(uri);
synchronized(filesystems) {
Path realPath = null;
if (ensureFile(path)) {
realPath = path.toRealPath();
if (filesystems.containsKey(realPath))
throw new FileSystemAlreadyExistsException();
}
ZipFileSystem zipfs = null;
try {
zipfs = new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
// assume NOT a zip/jar file
throw new UnsupportedOperationException();
}
filesystems.put(realPath, zipfs);
return zipfs;
}
}
@Override
public FileSystem newFileSystem(Path path, Map<String, ?> env)
throws IOException
{
if (path.getFileSystem() != FileSystems.getDefault()) {
throw new UnsupportedOperationException();
}
ensureFile(path);
try {
return new ZipFileSystem(this, path, env);
} catch (ZipError ze) {
String pname = path.toString();
if (pname.endsWith(".zip") || pname.endsWith(".jar"))
throw ze;
throw new UnsupportedOperationException();
}
}
/**
* Gets a set of files that are contained in an archive
*
* @param archiveAbsolutePath The absolute path to the archive
* @return A set of files (not directories) that are contained in the zip file
* @throws IOException If there is an error reading the archive
*/
public static ImmutableSet<Path> getZipMembers(Path archiveAbsolutePath) throws IOException {
try (FileSystem zipFs = FileSystems.newFileSystem(archiveAbsolutePath, null)) {
Path root = Iterables.getOnlyElement(zipFs.getRootDirectories());
return Files.walk(root)
.filter(path -> !Files.isDirectory(path))
.map(root::relativize)
.map(path -> Paths.get(path.toString())) // Clear the filesystem from the path
.collect(ImmutableSet.toImmutableSet());
} catch (ZipError error) {
// For some reason the zip filesystem support throws an error when an IOException would do
// just as well.
throw new IOException(
String.format("Could not read %s because of %s", archiveAbsolutePath, error.toString()),
error);
}
}
/**
* This call is thread safe, a new FS is created for each invocation.
*
* @param path The zip file.
* @return A new FileSystem instance
* @throws IOException in case of a failure
*/
public static FileSystem newFileSystem(final Path path) throws IOException {
try {
return FileSystems.newFileSystem(path, (ClassLoader) null);
} catch (IOException | ZipError ioe) {
// TODO: (at a later date) Get rid of the ZipError catching (and instead only catch IOException)
// since it's a JDK bug which threw the undeclared ZipError instead of an IOException.
// Java 9 fixes it https://bugs.openjdk.java.net/browse/JDK-8062754
// include the path for which the filesystem creation failed
throw new IOException("Failed to create a new filesystem for " + path, ioe);
}
}
static void zerror(String msg) {
throw new ZipError(msg);
}
static void zerror(String msg) {
throw new ZipError(msg);
}