Demo.java revision 4378
/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This source code is provided to illustrate the usage of a given feature
* or technique and has been deliberately simplified. Additional steps
* required for a production-quality application, such as security checks,
* input validation and proper error handling, might not be present in
* this sample code.
*/
/*
* ZipFileSystem usage demo
*
* java Demo action ZipfileName [...]
*
* @author Xueming Shen
*/
public class Demo {
static enum Action {
rename, // <java Demo rename zipfile src dst>
// rename entry src to dst inside zipfile
movein, // <java Demo movein zipfile src dst>
// move an external src file into zipfile
// as entry dst
moveout, // <java Demo moveout zipfile src dst>
// move a zipfile entry src out to dst
copy, // <java Demo copy zipfile src dst>
// copy entry src to dst inside zipfile
copyin, // <java Demo copyin zipfile src dst>
// copy an external src file into zipfile
// as entry dst
copyin_attrs, // <java Demo copyin_attrs zipfile src dst>
// copy an external src file into zipfile
// as entry dst, with attributes (timestamp)
copyout, // <java Demo copyout zipfile src dst>
// copy zipfile entry src" out to file dst
copyout_attrs, // <java Demo copyout_attrs zipfile src dst>
zzmove, // <java Demo zzmove zfsrc zfdst path>
zzcopy, // <java Demo zzcopy zfsrc zfdst path>
// copy path from zipfile zfsrc to zipfile
// zfdst
attrs, // <java Demo attrs zipfile path>
// printout the attributes of entry path
attrsspace, // <java Demo attrsspace zipfile path>
// printout the storespace attrs of entry path
// set the lastModifiedTime of entry path
lsdir, // <java Demo lsdir zipfile dir>
mkdir, // <java Demo mkdir zipfile dir>
mkdirs, // <java Demo mkdirs zipfile dir>
rmdirs, // <java Demo rmdirs zipfile dir>
list, // <java Demo list zipfile [dir]>
// recursively list all entries of dir
// via DirectoryStream
tlist, // <java Demo tlist zipfile [dir]>
// list with buildDirTree=true
vlist, // <java Demo vlist zipfile [dir]>
// recursively verbose list all entries of
// dir via DirectoryStream
walk, // <java Demo walk zipfile [dir]>
// recursively walk all entries of dir
// via Files.walkFileTree
twalk, // <java Demo twalk zipfile [dir]>
// walk with buildDirTree=true
extract, // <java Demo extract zipfile file [...]>
update, // <java Demo extract zipfile file [...]>
delete, // <java Demo delete zipfile file [...]>
add, // <java Demo add zipfile file [...]>
create, // <java Demo create zipfile file [...]>
// create a new zipfile if it doesn't exit
// and then add the file(s) into it.
attrs2, // <java Demo attrs2 zipfile file [...]>
// test different ways to print attrs
prof,
}
}
switch (action) {
case rename:
break;
case moveout:
break;
case movein:
break;
case copy:
break;
case copyout:
break;
case copyin:
break;
case copyin_attrs:
break;
case copyout_attrs:
break;
case zzmove:
}
break;
case zzcopy:
}
break;
case attrs:
}
break;
case setmtime:
}
break;
case setctime:
}
break;
case setatime:
}
break;
case attrsspace:
break;
case list:
case tlist:
else
break;
case vlist:
else
break;
case twalk:
case walk:
break;
case extract:
} else {
}
}
break;
case delete:
break;
case create:
case add:
case update:
}
break;
case lsdir:
}
}))
{
}
break;
case mkdir:
break;
case mkdirs:
break;
case attrs2:
}
}
}
break;
case prof:
while (true) {
//list(fs.getPath("/"), true);
}
}
} catch (Exception x) {
x.printStackTrace();
}
}
private static FileSystemProvider getZipFSProvider() {
return provider;
}
return null;
}
@SuppressWarnings("unused")
/**
* Not used in demo, but included for demonstrational purposes.
*/
}
@SuppressWarnings("unused")
/**
* Not used in demo, but included for demonstrational purposes.
*/
}
{
path,
new SimpleFileVisitor<Path>() {
private int indent = 0;
private void indent() {
int n = 0;
while (n++ < indent)
}
{
indent();
return FileVisitResult.CONTINUE;
}
{
indent();
indent += 2;
return FileVisitResult.CONTINUE;
}
{
indent -= 2;
return FileVisitResult.CONTINUE;
}
});
}
}
} else {
}
}
}
} else {
}
}
// use DirectoryStream
throws IOException
{
try {
} catch (FileAlreadyExistsException x) {}
}
}
}
} else {
//System.out.println("copying..." + path);
}
}
// use TreeWalk to move
throws IOException
{
{
try {
} catch (IOException x) {
x.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
{
try {
} catch (IOException x) {
x.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
throws IOException
{
try {
} catch (IOException x) {
//x.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
});
}
}
}
@SuppressWarnings("unused")
/**
* Not used in demo, but included for demonstrational purposes.
*/
}
}
if (verbose)
}
return;
}
}
}
@SuppressWarnings("unused")
/**
* Checks that the content of two paths are equal.
* Not used in demo, but included for demonstrational purposes.
*/
{
//System.out.printf("checking <%s> vs <%s>...%n",
// src.toString(), dst.toString());
//streams
byte[] bufSrc = new byte[8192];
byte[] bufDst = new byte[8192];
{
int nSrc = 0;
int nDst = 0;
if (n == -1) {
throw new RuntimeException("CHECK FAILED!");
}
nDst += n;
}
while (--nSrc >= 0) {
throw new RuntimeException("CHECK FAILED!");
}
nSrc--;
}
}
}
// channels
{
throw new RuntimeException("CHECK FAILED!");
}
int nSrc = 0;
throw new RuntimeException("CHECK FAILED!");
}
while (--nSrc >= 0) {
throw new RuntimeException("CHECK FAILED!");
}
nSrc--;
}
}
} catch (IOException x) {
x.printStackTrace();
}
}
{
{
}
}
}
{
{
}
}
}
{
byte[] buf = new byte[8192];
{
int n = 0;
}
}
}
}