forked from mthli/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipTest.java
More file actions
58 lines (53 loc) · 1.67 KB
/
ZipTest.java
File metadata and controls
58 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package zip;
import java.io.*;
import java.nio.charset.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.*;
import java.util.zip.*;
/**
* @version 1.4 2012-06-01
* @author Cay Horstmann
*/
public class ZipTest
{
public static void main(String[] args) throws IOException
{
String zipname = args[0];
showContents(zipname);
System.out.println("---");
showContents2(zipname);
}
public static void showContents(String zipname) throws IOException
{
// Here, we use the classic zip API
try (ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname)))
{
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null)
{
System.out.println(entry.getName());
Scanner in = new Scanner(zin);
while (in.hasNextLine())
System.out.println(" " + in.nextLine());
// DO NOT CLOSE in
zin.closeEntry();
}
}
}
public static void showContents2(String zipname) throws IOException
{
// Here, we make a Java SE 7 file system
FileSystem fs = FileSystems.newFileSystem(Paths.get(zipname), null);
Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>()
{
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException
{
System.out.println(path);
for (String line : Files.readAllLines(path, Charset.forName("UTF-8")))
System.out.println(" " + line);
return FileVisitResult.CONTINUE;
}
});
}
}