-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZipFileInfoUtil.java
149 lines (134 loc) · 6.62 KB
/
ZipFileInfoUtil.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.j256.simplezip.examples;
import java.io.File;
import java.io.IOException;
import com.j256.simplezip.ZipFileInput;
import com.j256.simplezip.format.ExternalFileAttributesUtils;
import com.j256.simplezip.format.ZipCentralDirectoryEnd;
import com.j256.simplezip.format.ZipCentralDirectoryFileEntry;
import com.j256.simplezip.format.ZipDataDescriptor;
import com.j256.simplezip.format.ZipFileHeader;
/**
* Displays information about a Zip file in excruciating detail.
*
* @author graywatson
*/
public class ZipFileInfoUtil {
private static final String INDENT = " ";
public static void main(String[] args) throws IOException {
new ZipFileInfoUtil().doMain(args);
}
public void doMain(String[] args) throws IOException {
if (args.length == 0) {
System.err.println("No zip, jar, or war files specified on the command line");
System.exit(1);
return;
}
for (String arg : args) {
File file = new File(arg);
if (!file.exists()) {
System.err.println("ERROR: file does not exist: " + file);
continue;
}
// open up our Zip file
try (ZipFileInput zipInput = new ZipFileInput(file);) {
System.out.println("Information about " + file + ":");
displayFileEntries(zipInput);
displayCentralDirectoryFileEntries(zipInput);
displayCentralDirectoryEnd(zipInput);
}
}
}
/**
* Display each of the file headers with its details. It does not process any of the file data.
*/
private void displayFileEntries(ZipFileInput zipInput) throws IOException {
while (true) {
// read the next file file-header, null when done
ZipFileHeader fileHeader = zipInput.readFileHeader();
if (fileHeader == null) {
break;
}
System.out.println(INDENT + "file-name: " + fileHeader.getFileName());
System.out.println(INDENT + INDENT + "version-needed: " + fileHeader.getVersionNeededMajorMinorString()
+ " (" + fileHeader.getVersionNeeded() + ")");
System.out.println(INDENT + INDENT + "flags: " + fileHeader.getGeneralPurposeFlagsAsEnums() + " (0x"
+ Integer.toHexString(fileHeader.getGeneralPurposeFlags()) + ")");
System.out.println(INDENT + INDENT + "compression-method: " + fileHeader.getCompressionMethodAsEnum() + " ("
+ fileHeader.getCompressionMethod() + ")");
System.out.println(INDENT + INDENT + "last-modified: " + fileHeader.getLastModifiedDateTime() + " (date "
+ fileHeader.getLastModifiedDate() + ", time " + fileHeader.getLastModifiedTime() + ")");
System.out.println(INDENT + INDENT + "crc checksum: " + fileHeader.getCrc32());
printSizes(INDENT + INDENT, fileHeader.getCompressedSize(), fileHeader.getUncompressedSize());
System.out.println(INDENT + INDENT + "extra-bytes: " + fileHeader.getExtraFieldBytes().length);
zipInput.skipFileData();
// spit out the optional data-descriptor
ZipDataDescriptor dataDescriptor = zipInput.getCurrentDataDescriptor();
if (dataDescriptor != null) {
System.out.println(INDENT + INDENT + "data-descriptor:");
System.out.println(INDENT + INDENT + INDENT + "crc checksum: " + dataDescriptor.getCrc32());
printSizes(INDENT + INDENT + INDENT, dataDescriptor.getCompressedSize(),
dataDescriptor.getUncompressedSize());
}
}
}
/**
* Display each of the central-directory file-entries.
*/
private void displayCentralDirectoryFileEntries(ZipFileInput zipInput) throws IOException {
while (true) {
// read the next file-entry, null when done
ZipCentralDirectoryFileEntry dirEntry = zipInput.readDirectoryFileEntry();
if (dirEntry == null) {
break;
}
System.out.println(INDENT + "directory-entry: " + dirEntry.getFileName());
System.out.println(INDENT + INDENT + "version-made: platform " + dirEntry.getPlatformMade() + ", version "
+ dirEntry.getVersionMadeMajorMinorString() + " (" + dirEntry.getVersionMade() + ")");
System.out.println(INDENT + INDENT + "version-needed: " + dirEntry.getVersionNeededMajorMinorString() + " ("
+ dirEntry.getVersionNeeded() + ")");
System.out.println(INDENT + INDENT + "flags: " + dirEntry.getGeneralPurposeFlagsAsEnums() + " (0x"
+ Integer.toHexString(dirEntry.getGeneralPurposeFlags()) + ")");
System.out.println(INDENT + INDENT + "compression-method: " + dirEntry.getCompressionMethodAsEnum() + " ("
+ dirEntry.getCompressionMethod() + ")");
System.out.println(INDENT + INDENT + "last-modified: " + dirEntry.getLastModifiedDateTime() + " (date "
+ dirEntry.getLastModifiedDate() + ", time " + dirEntry.getLastModifiedTime() + ")");
System.out.println(INDENT + INDENT + "crc checksum: " + dirEntry.getCrc32());
printSizes(INDENT + INDENT, dirEntry.getCompressedSize(), dirEntry.getUncompressedSize());
System.out.println(INDENT + INDENT + "disk number start: " + dirEntry.getDiskNumberStart());
System.out.println(INDENT + INDENT + "internal file attributes: 0x"
+ Integer.toHexString(dirEntry.getInternalFileAttributes()));
System.out.println(INDENT + INDENT + "external file attributes: "
+ ExternalFileAttributesUtils.toString(dirEntry.getExternalFileAttributes()) + " (0x"
+ Integer.toHexString(dirEntry.getExternalFileAttributes()) + ")");
System.out.println(
INDENT + INDENT + "relative offset of local header: " + dirEntry.getRelativeOffsetOfLocalHeader());
System.out.println(INDENT + INDENT + "extra-bytes: " + dirEntry.getExtraFieldBytes().length);
System.out.println(INDENT + INDENT + "comment: " + dirEntry.getComment());
}
}
/**
* Read in the Zip end structure.
*/
private void displayCentralDirectoryEnd(ZipFileInput zipInput) throws IOException {
ZipCentralDirectoryEnd dirEnd = zipInput.readDirectoryEnd();
System.out.println(INDENT + "directory-end:");
System.out.println(INDENT + INDENT + "disk number: " + dirEnd.getDiskNumber());
System.out.println(INDENT + INDENT + "disk number start: " + dirEnd.getDiskNumberStart());
System.out.println(INDENT + INDENT + "# records on disk: " + dirEnd.getNumRecordsOnDisk());
System.out.println(INDENT + INDENT + "# records total: " + dirEnd.getNumRecordsTotal());
System.out.println(INDENT + INDENT + "directory size: " + dirEnd.getDirectorySize());
System.out.println(INDENT + INDENT + "directory offset: " + dirEnd.getDirectoryOffset());
System.out.println(INDENT + INDENT + "comment: " + dirEnd.getComment());
}
/**
* Print sizes and the compression reduction.
*/
private void printSizes(String indent, long compressed, long uncompressed) {
float reduction = 0;
if (uncompressed != 0) {
reduction = Math.round(1000.0F * (1.0F - ((float) compressed / ((float) uncompressed)))) / 10.0F;
}
System.out.println(indent + "sizes: " + uncompressed + " uncompressed, " + compressed + " compressed ("
+ reduction + "% reduction)");
}
}