34 import java.io.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.FileOutputStream;
37
38 public class ManyZipFiles {
39 static final int numFiles = 3000;
40
41 public static void realMain(String[] args) throws Throwable {
42 // Linux does not yet allow opening this many files; Solaris
43 // 8 requires an explicit allocation of more file descriptors
44 // to succeed. Since this test is written to check for a
45 // Windows capability it is much simpler to only run it
46 // on that platform.
47 String osName = System.getProperty("os.name");
48 if (osName.startsWith("Linux") || osName.startsWith("SunOS")) {
49 return;
50 }
51
52 // Create some zip data
53 ByteArrayOutputStream baos = new ByteArrayOutputStream();
54 ZipOutputStream zos = new ZipOutputStream(baos);
55 ZipEntry ze = new ZipEntry("test");
56 zos.putNextEntry(ze);
57 byte[] hello = "hello, world".getBytes("ASCII");
58 zos.write(hello, 0, hello.length);
59 zos.closeEntry();
60 zos.finish();
61 zos.close();
62 byte[] data = baos.toByteArray();
63
64 ZipFile zips[] = new ZipFile[numFiles];
65
66 try {
67 // Create a directory for zip files created below
68 File tmpdir = new File(
69 System.getProperty("java.io.tmpdir")
70 + File.separator + "ManyZipFiles");
71 if (tmpdir.exists() && !tmpdir.isDirectory()) {
72 fail(tmpdir.getAbsolutePath()
73 + " already exists but is not a directory");
74 return;
75 }
76 if (!tmpdir.exists()) {
77 if (!tmpdir.mkdirs()) {
78 fail("Couldn't create directory "
79 + tmpdir.getAbsolutePath() + " for test files");
80 return;
81 }
82 } else if (!tmpdir.canWrite()) {
83 fail("Don't have write access for directory "
84 + tmpdir.getAbsolutePath() + " for test files");
85 return;
86 }
87 tmpdir.deleteOnExit();
88
89 // Create and then open a large number of zip files
90 for (int i = 0; i < numFiles; i++) {
91 File f = File.createTempFile("test", ".zip", tmpdir);
92 f.deleteOnExit();
93 FileOutputStream fos = new FileOutputStream(f);
94 fos.write(data, 0, data.length);
95 fos.close();
96 try {
97 zips[i] = new ZipFile(f);
98 } catch (Throwable t) {
99 fail("Failed to open zip file #" + i + " named "
100 + zips[i].getName());
101 throw t;
102 }
103 }
104 } finally {
105 // This finally block is due to bug 4171239. On windows, if the
106 // file is still open at the end of the VM, deleteOnExit won't
107 // take place. "new ZipFile(...)" opens the zip file, so we have
108 // to explicity close those opened above. This finally block can
109 // be removed when 4171239 is fixed.
110 for (int i = 0; i < numFiles; i++) {
111 if (zips[i] != null) {
112 try {
113 zips[i].close();
114 } catch (Throwable t) {
115 fail("At zip[" + i + "] named " + zips[i].getName()
116 + " caught " + t);
117 }
118 }
119 }
120 }
121 pass();
122 }
123
124 //--------------------- Infrastructure ---------------------------
125 static volatile int passed = 0, failed = 0;
126 static void pass() {passed++;}
127 static void fail() {failed++; Thread.dumpStack();}
128 static void fail(String msg) {System.out.println(msg); fail();}
129 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
34 import java.io.ByteArrayOutputStream;
35 import java.io.File;
36 import java.io.FileOutputStream;
37
38 public class ManyZipFiles {
39 static final int numFiles = 3000;
40
41 public static void realMain(String[] args) throws Throwable {
42 // Linux does not yet allow opening this many files; Solaris
43 // 8 requires an explicit allocation of more file descriptors
44 // to succeed. Since this test is written to check for a
45 // Windows capability it is much simpler to only run it
46 // on that platform.
47 String osName = System.getProperty("os.name");
48 if (osName.startsWith("Linux") || osName.startsWith("SunOS")) {
49 return;
50 }
51
52 // Create some zip data
53 ByteArrayOutputStream baos = new ByteArrayOutputStream();
54 try (ZipOutputStream zos = new ZipOutputStream(baos)) {
55 ZipEntry ze = new ZipEntry("test");
56 zos.putNextEntry(ze);
57 byte[] hello = "hello, world".getBytes("ASCII");
58 zos.write(hello, 0, hello.length);
59 zos.closeEntry();
60 zos.finish();
61 }
62 byte[] data = baos.toByteArray();
63
64 ZipFile zips[] = new ZipFile[numFiles];
65
66 try {
67 // Create a directory for zip files created below
68 File tmpdir = new File(
69 System.getProperty("java.io.tmpdir")
70 + File.separator + "ManyZipFiles");
71 if (tmpdir.exists() && !tmpdir.isDirectory()) {
72 fail(tmpdir.getAbsolutePath()
73 + " already exists but is not a directory");
74 return;
75 }
76 if (!tmpdir.exists()) {
77 if (!tmpdir.mkdirs()) {
78 fail("Couldn't create directory "
79 + tmpdir.getAbsolutePath() + " for test files");
80 return;
81 }
82 } else if (!tmpdir.canWrite()) {
83 fail("Don't have write access for directory "
84 + tmpdir.getAbsolutePath() + " for test files");
85 return;
86 }
87 tmpdir.deleteOnExit();
88
89 // Create and then open a large number of zip files
90 for (int i = 0; i < numFiles; i++) {
91 File f = File.createTempFile("test", ".zip", tmpdir);
92 f.deleteOnExit();
93 try (FileOutputStream fos = new FileOutputStream(f)) {
94 fos.write(data, 0, data.length);
95 }
96 try {
97 zips[i] = new ZipFile(f);
98 } catch (Throwable t) {
99 fail("Failed to open zip file #" + i + " named "
100 + zips[i].getName());
101 throw t;
102 }
103 }
104 } finally {
105 // This finally block is due to bug 4171239. On Windows, if the
106 // file is still open at the end of the VM, deleteOnExit won't
107 // take place. "new ZipFile(...)" opens the zip file, so we have
108 // to explicitly close those opened above. This finally block can
109 // be removed when 4171239 is fixed. See also 6357433, against which
110 // 4171239 was closed as a duplicate.
111 for (int i = 0; i < numFiles; i++) {
112 if (zips[i] != null) {
113 try {
114 zips[i].close();
115 } catch (Throwable t) {
116 fail("At zip[" + i + "] named " + zips[i].getName()
117 + " caught " + t);
118 }
119 }
120 }
121 }
122 pass();
123 }
124
125 //--------------------- Infrastructure ---------------------------
126 static volatile int passed = 0, failed = 0;
127 static void pass() {passed++;}
128 static void fail() {failed++; Thread.dumpStack();}
129 static void fail(String msg) {System.out.println(msg); fail();}
130 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|