@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); } }
static void zerror(String msg) { throw new ZipError(msg); }
@Override public String probeContentType(Path path) throws IOException { ByteBuffer buf = ByteBuffer.allocate(256); try (SeekableByteChannel byteChannel = Files.newByteChannel(path, StandardOpenOption.READ)) { int read = byteChannel.read(buf); if (read < 38) { return null; } ; } buf.flip(); // Look for PK byte[] firstBytes = buf.array(); String pk = new String(firstBytes, 0, 2, LATIN1); if (!(pk.equals("PK") && firstBytes[2] == 3 && firstBytes[3] == 4)) { // Did not match magic numbers of ZIP as specified in ePub OCF // http://www.idpf.org/epub/30/spec/epub30-ocf.html#app-media-type return null; } String mimetype = new String(firstBytes, 30, 8, LATIN1); if (!mimetype.equals(MIMETYPE)) { return APPLICATION_ZIP; } // Read the 'mimetype' file. try (ZipInputStream is = new ZipInputStream(new ByteArrayInputStream( firstBytes))) { ZipEntry entry = is.getNextEntry(); if (!MIMETYPE.equals(entry.getName())) { return APPLICATION_ZIP; } byte[] mediaTypeBuffer = new byte[256]; int size = is.read(mediaTypeBuffer); if (size < 1) { return APPLICATION_ZIP; } return new String(mediaTypeBuffer, 0, size, ASCII); } catch (ZipException | ZipError e) { return null; } }
public static FatalErrorHandler exitOnJvmError() { return exitOnMatchedError(singletonList(Error.class), asList(AssertionError.class, StackOverflowError.class, IOError.class, ZipError.class)); }
/** * @tests {@link java.util.zip.ZipError#ZipError(String)} */ public void test_constructor() { ZipError error = new ZipError("ZipError"); assertEquals("ZipError", error.getMessage()); }
/** * @tests java.util.zip.ZipError#Serialization() */ public void test_serialization() throws Exception { ZipError error = new ZipError("serialization test"); SerializationTest.verifySelf(error); }
/** * @tests serialization/deserialization compatibility with RI. */ public void testSerializationCompatibility() throws Exception { ZipError error = new ZipError("serialization test"); SerializationTest.verifyGolden(this, error); }