提交 47d11269 编写于 作者: S sherman

6989148: (fs) zip provider should be available "out of the box"

Summary: zip filesystem provider update, add zipfs.jar into ext dir
Reviewed-by: alanb
上级 53b2d360
......@@ -33,8 +33,8 @@ DEMONAME = zipfs
include $(BUILDDIR)/common/Defs.gmk
DEMO_ROOT = $(SHARE_SRC)/demo/nio/$(DEMONAME)
DEMO_TOPFILES = ./README.txt
DEMO_SRCDIR = $(DEMO_ROOT)
DEMO_TOPFILES = README.txt Demo.java
DEMO_SRCDIR = $(DEMO_ROOT)/src
DEMO_DESTDIR = $(DEMODIR)/nio/$(DEMONAME)
#
......@@ -42,10 +42,10 @@ DEMO_DESTDIR = $(DEMODIR)/nio/$(DEMONAME)
#
include $(BUILDDIR)/common/Demo.gmk
#EXTJAR = $(EXTDIR)/$(DEMONAME).jar
#
#all : build $(EXTJAR)
#
#$(EXTJAR) : $(DEMO_JAR)
# $(prep-target)
# $(CP) $(DEMO_JAR) $(EXTJAR)
EXTJAR = $(EXTDIR)/$(DEMONAME).jar
all : build $(EXTJAR)
$(EXTJAR) : $(DEMO_JAR)
$(prep-target)
$(CP) $(DEMO_JAR) $(EXTJAR)
......@@ -45,12 +45,7 @@ import static java.nio.file.StandardCopyOption.*;
/*
* ZipFileSystem usage demo
*
* java [-cp .../zipfs.jar:./] Demo action ZipfileName [...]
*
* To deploy the provider, either copy the zipfs.jar into JDK/JRE
* extensions directory or add
* <JDK_HOME>/demo/nio/ZipFileSystem/zipfs.jar
* into your class path as showed above.
* java Demo action ZipfileName [...]
*
* @author Xueming Shen
*/
......@@ -153,14 +148,11 @@ public class Demo {
Action action = Action.valueOf(args[0]);
Map<String, Object> env = env = new HashMap<>();
if (action == Action.create)
env.put("createNew", true);
env.put("create", "true");
if (action == Action.tlist || action == Action.twalk)
env.put("buildDirTree", true);
FileSystem fs = FileSystems.newFileSystem(Paths.get(args[1]), env, null);
FileSystem fs = FileSystems.newFileSystem(
URI.create("zip" + Paths.get(args[1]).toUri().toString().substring(4)),
env,
null);
try {
FileSystem fs2;
Path path, src, dst;
......@@ -207,19 +199,13 @@ public class Demo {
src.copyTo(dst, COPY_ATTRIBUTES);
break;
case zzmove:
fs2 = FileSystems.newFileSystem(
URI.create("zip" + Paths.get(args[2]).toUri().toString().substring(4)),
env,
null);
fs2 = FileSystems.newFileSystem(Paths.get(args[2]), env, null);
//sf1.getPath(args[3]).moveTo(fs2.getPath(args[3]));
z2zmove(fs, fs2, args[3]);
fs2.close();
break;
case zzcopy:
fs2 = FileSystems.newFileSystem(
URI.create("zip" + Paths.get(args[2]).toUri().toString().substring(4)),
env,
null);
fs2 = FileSystems.newFileSystem(Paths.get(args[2]), env, null);
//sf1.getPath(args[3]).copyTo(fs2.getPath(args[3]));
z2zcopy(fs, fs2, args[3]);
fs2.close();
......
ZipFileSystem is a file system provider that treats the contents of a zip or
JAR file as a java.nio.file.FileSystem.
To deploy the provider you must copy zipfs.jar into your extensions
directory or else add <JDK_HOME>/demo/nio/zipfs/zipfs.jar
to your class path.
The factory methods defined by the java.nio.file.FileSystems class can be
used to create a FileSystem, eg:
......@@ -15,9 +11,9 @@ used to create a FileSystem, eg:
-or
// locate file system by URI
// locate file system by the legacy JAR URL syntax
Map<String,?> env = Collections.emptyMap();
URI uri = URI.create("zip:///mydir/foo.jar");
URI uri = URI.create("jar:file:/mydir/foo.jar");
FileSystem fs = FileSystems.newFileSystem(uri, env);
Once a FileSystem is created then classes in the java.nio.file package
......@@ -26,4 +22,6 @@ can be used to access files in the zip/JAR file, eg:
Path mf = fs.getPath("/META-INF/MANIFEST.MF");
InputStream in = mf.newInputStream();
See Demo.java for more interesting usages.
......@@ -91,11 +91,11 @@ public class ZipFileSystem extends FileSystem {
throws IOException
{
// configurable env setup
this.createNew = "true".equals(env.get("create"));
this.nameEncoding = env.containsKey("encoding") ?
(String)env.get("encoding") : "UTF-8";
this.buildDirTree = TRUE.equals(env.get("buildDirTreea"));
this.useTempFile = TRUE.equals(env.get("useTempFile"));
this.createNew = TRUE.equals(env.get("createNew"));
this.nameEncoding = env.containsKey("nameEncoding") ?
(String)env.get("nameEncoding") : "UTF-8";
this.defaultDir = env.containsKey("default.dir") ?
(String)env.get("default.dir") : "/";
if (this.defaultDir.charAt(0) != '/')
......@@ -1176,7 +1176,9 @@ public class ZipFileSystem extends FileSystem {
} else {
os.write(buf, 0, LOCHDR); // write out the loc header
locoff += LOCHDR;
size += LOCNAM(buf) + LOCEXT(buf) + LOCSIZ(buf);
// use e.csize, LOCSIZ(buf) is zero if FLAG_DATADESCR is on
// size += LOCNAM(buf) + LOCEXT(buf) + LOCSIZ(buf);
size += LOCNAM(buf) + LOCEXT(buf) + e.csize;
written = LOCHDR + size;
}
int n;
......
......@@ -63,7 +63,7 @@ public class ZipFileSystemProvider extends FileSystemProvider {
@Override
public String getScheme() {
return "zip";
return "jar";
}
protected Path uriToPath(URI uri) {
......@@ -72,10 +72,14 @@ public class ZipFileSystemProvider extends FileSystemProvider {
throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'");
}
try {
return Paths.get(new URI("file", uri.getHost(), uri.getPath(), null))
.toAbsolutePath();
// only support legacy JAR URL syntax jar:{uri}!/{entry} for now
String spec = uri.getSchemeSpecificPart();
int sep = spec.indexOf("!/");
if (sep != -1)
spec = spec.substring(0, sep);
return Paths.get(new URI(spec)).toAbsolutePath();
} catch (URISyntaxException e) {
throw new AssertionError(e); //never thrown
throw new IllegalArgumentException(e.getMessage(), e);
}
}
......@@ -119,14 +123,14 @@ public class ZipFileSystemProvider extends FileSystemProvider {
@Override
public Path getPath(URI uri) {
FileSystem fs = getFileSystem(uri);
String fragment = uri.getFragment();
if (fragment == null) {
String spec = uri.getSchemeSpecificPart();
int sep = spec.indexOf("!/");
if (sep == -1)
throw new IllegalArgumentException("URI: "
+ uri
+ " does not contain path fragment ex. zip:///c:/foo.zip#/BAR");
}
return fs.getPath(fragment);
+ " does not contain path info ex. jar:file:/c:/foo.zip!/BAR");
return getFileSystem(uri).getPath(spec.substring(sep + 1));
}
@Override
......
......@@ -33,9 +33,7 @@ package com.sun.nio.zipfs;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import com.sun.nio.zipfs.ZipFileSystem.Entry;
import static com.sun.nio.zipfs.ZipConstants.*;
import static com.sun.nio.zipfs.ZipUtils.*;
......@@ -172,7 +170,7 @@ public class ZipInfo {
static void printExtra(byte[] extra, int off, int len) {
int end = off + len;
while (off + 4 < end) {
while (off + 4 <= end) {
int tag = SH(extra, off);
int sz = SH(extra, off + 2);
print(" [tag=0x%04x, sz=%d, data= ", tag, sz);
......
......@@ -191,13 +191,12 @@ public class ZipPath extends Path {
@Override
public URI toUri() {
String zfPath = zfs.toString();
if (File.separatorChar == '\\') // replace all separators by '/'
zfPath = "/" + zfPath.replace("\\", "/");
try {
return new URI("zip", "",
zfPath,
zfs.getString(toAbsolutePath().path));
return new URI("jar",
zfs.getZipFile().toUri() +
"!" +
zfs.getString(toAbsolutePath().path),
null);
} catch (Exception ex) {
throw new AssertionError(ex);
}
......
此差异已折叠。
......@@ -40,24 +40,24 @@ public class Basic {
boolean found = false;
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
if (provider.getScheme().equalsIgnoreCase("zip")) {
if (provider.getScheme().equalsIgnoreCase("jar")) {
found = true;
break;
}
}
if (!found)
throw new RuntimeException("'zip' provider not installed");
throw new RuntimeException("'jar' provider not installed");
// Test: FileSystems#newFileSystem(FileRef)
Map<String,?> env = new HashMap<String,Object>();
FileSystems.newFileSystem(zipfile, env, null).close();
// Test: FileSystems#newFileSystem(URI)
URI uri = URI.create("zip" + zipfile.toUri().toString().substring(4));
URI uri = new URI("jar", zipfile.toUri().toString(), null);
FileSystem fs = FileSystems.newFileSystem(uri, env, null);
// Test: exercise toUri method
String expected = uri.toString() + "#/foo";
String expected = uri.toString() + "!/foo";
String actual = fs.getPath("/foo").toUri().toString();
if (!actual.equals(expected)) {
throw new RuntimeException("toUri returned '" + actual +
......
......@@ -58,7 +58,7 @@ public class ZipFSTester {
// clone a fs and test on it
Path tmpfsPath = getTempPath();
Map<String, Object> env = new HashMap<String, Object>();
env.put("createNew", true);
env.put("create", "true");
FileSystem fs0 = newZipFileSystem(tmpfsPath, env);
z2zcopy(fs, fs0, "/", 0);
fs0.close(); // sync to file
......@@ -147,7 +147,7 @@ public class ZipFSTester {
// create a new filesystem, copy everything from fs
Map<String, Object> env = new HashMap<String, Object>();
env.put("createNew", true);
env.put("create", "true");
FileSystem fs0 = newZipFileSystem(fs1Path, env);
final FileSystem fs2 = newZipFileSystem(fs2Path, env);
......@@ -282,11 +282,7 @@ public class ZipFSTester {
private static FileSystem newZipFileSystem(Path path, Map<String, ?> env)
throws IOException
{
return FileSystems.newFileSystem(
URI.create("zip" +
path.toUri().toString().substring(4)),
env,
null);
return FileSystems.newFileSystem(path, env, null);
}
private static Path getTempPath() throws IOException
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册