blob: 6d803857f8813044652ce2067c144ed67992e2cd (
plain)
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
|
package test;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class SumUpClasses {
public static void main(String[] args) {
try {
File dir = new File("C:\\revjava\\remote\\data\\input\\");
long[] res = getClasses(dir);
System.out.println("Count: "+res[0]);
System.out.println("Size: "+res[1]/1024/1024);
} catch(Exception ex) {
ex.printStackTrace();
}
}
private static long[] getClasses(File file) {
if(file.isDirectory()) {
long count = 0, size = 0;
for(File f : file.listFiles()) {
long[] arr = getClasses(f);
count+=arr[0];
size+=arr[1];
}
return new long[] {count, size};
} else {
String filename = file.getName();
if(filename.endsWith(".class")) {
return new long[] {1, file.length()};
} else if(filename.endsWith(".zip")) {
try {
return getClassesZip(new ZipFile(file));
} catch(IOException ex) {
System.out.println("Cannot read file: " + file.getAbsolutePath());
}
} else if(filename.endsWith(".jar")) {
try {
return getClassesZip(new JarFile(file));
} catch(IOException ex) {
System.out.println("Cannot read file: " + file.getAbsolutePath());
}
}
}
return new long[] {0, 0};
}
private static long[] getClassesZip(ZipFile archive) {
long count = 0, size = 0;
Enumeration<? extends ZipEntry> en = archive.entries();
while(en.hasMoreElements()) {
ZipEntry entr = (ZipEntry)en.nextElement();
if(!entr.isDirectory() && entr.getName().endsWith(".class")) {
count++;
size+=entr.getSize();
}
}
return new long[] {count, size};
}
}
|