1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.vfs2.provider.zip;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.charset.StandardCharsets;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.io.IOUtils;
27 import org.apache.commons.vfs2.FileObject;
28 import org.apache.commons.vfs2.FileSystemException;
29 import org.apache.commons.vfs2.FileSystemManager;
30 import org.apache.commons.vfs2.VFS;
31 import org.junit.Assert;
32 import org.junit.Ignore;
33 import org.junit.Test;
34
35 public class ZipFileObjectTestCase {
36
37 private static final String NESTED_FILE_1 = "/read-xml-tests/file1.xml";
38 private static final String NESTED_FILE_2 = "/read-xml-tests/file2.xml";
39
40 private void assertDelete(final File fileObject) {
41 Assert.assertTrue("Could not delete file", fileObject.delete());
42 }
43
44 private File createTempFile() throws IOException {
45 final File zipFile = new File("src/test/resources/test-data/read-xml-tests.zip");
46 final File newZipFile = File.createTempFile(getClass().getSimpleName(), ".zip");
47 newZipFile.deleteOnExit();
48 FileUtils.copyFile(zipFile, newZipFile);
49 return newZipFile;
50 }
51
52 private void getInputStreamAndAssert(final FileObject fileObject, final String expectedId)
53 throws FileSystemException, IOException {
54 readAndAssert(fileObject, fileObject.getContent().getInputStream(), expectedId);
55 }
56
57 private void readAndAssert(final FileObject fileObject, final InputStream inputStream, final String expectedId)
58 throws IOException {
59 final String streamData = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
60 final String fileObjectString = fileObject.toString();
61 Assert.assertNotNull(fileObjectString, streamData);
62 Assert.assertEquals(
63 fileObjectString, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Root"
64 + expectedId + ">foo" + expectedId + "</Root" + expectedId + ">\r\n",
65 streamData);
66 }
67
68 private void resolveReadAssert(final FileObject zipFileObject, final String path)
69 throws IOException, FileSystemException {
70 try (final FileObject zipFileObject2 = zipFileObject.resolveFile(path)) {
71 try (final InputStream inputStream = zipFileObject2.getContent().getInputStream()) {
72 readAndAssert(zipFileObject2, inputStream, "2");
73 }
74 }
75 }
76
77
78
79
80
81
82
83 @Test
84 @Ignore("Shows that leaving a stream open and not closing any resource leaves the container file locked")
85 public void testLeaveNestedFileOpen() throws IOException {
86 final File newZipFile = createTempFile();
87 final FileSystemManager manager = VFS.getManager();
88 try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
89 @SuppressWarnings({ "resource" })
90 final FileObject zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
91 getInputStreamAndAssert(zipFileObject1, "1");
92 }
93 assertDelete(newZipFile);
94 }
95
96
97
98
99
100
101 @Test
102 public void testReadingFilesInZipFile() throws IOException {
103 final File newZipFile = createTempFile();
104 final FileSystemManager manager = VFS.getManager();
105 try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
106 try (final FileObject zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1)) {
107 try (final InputStream inputStream = zipFileObject1.getContent().getInputStream()) {
108 readAndAssert(zipFileObject1, inputStream, "1");
109 }
110 }
111 resolveReadAssert(zipFileObject, NESTED_FILE_2);
112 }
113 assertDelete(newZipFile);
114 }
115
116
117
118
119
120
121
122 @Test
123 public void testReadingOneAfterClosingAnotherFile() throws IOException {
124 final File newZipFile = createTempFile();
125 final FileSystemManager manager = VFS.getManager();
126 final FileObject zipFileObject1;
127 final InputStream inputStream1;
128 try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
129
130 zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
131 inputStream1 = zipFileObject1.getContent().getInputStream();
132 }
133
134 readAndAssert(zipFileObject1, inputStream1, "1");
135
136 zipFileObject1.close();
137 assertDelete(newZipFile);
138 }
139
140
141
142
143
144
145
146 @Test
147 public void testReadingOneAfterClosingAnotherStream() throws IOException {
148 final File newZipFile = createTempFile();
149 final FileSystemManager manager = VFS.getManager();
150 final FileObject zipFileObject1;
151 final InputStream inputStream1;
152 try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
153
154 zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
155 inputStream1 = zipFileObject1.getContent().getInputStream();
156 resolveReadAssert(zipFileObject, NESTED_FILE_2);
157 }
158
159
160 readAndAssert(zipFileObject1, inputStream1, "1");
161
162 zipFileObject1.close();
163 assertDelete(newZipFile);
164 }
165
166
167
168
169 @Test
170 public void testReadSpecialNameFileInZipFile() throws FileSystemException {
171
172 final File testFile = new File("src/test/resources/test-data/special_fileName.zip");
173 final String[] fileNames = {"file.txt", "file^.txt", "file~.txt", "file?.txt", "file@.txt", "file$.txt",
174 "file*.txt", "file&.txt", "file#.txt", "file%.txt", "file!.txt"};
175 final FileSystemManager manager = VFS.getManager();
176 final String baseUrl = "zip:file:"+testFile.getAbsolutePath();
177
178
179 try (final FileObject fileObject = manager.resolveFile(baseUrl)) {
180
181 Assert.assertEquals(fileObject.getChildren().length, fileNames.length);
182
183
184 for (final String fileName : fileNames) {
185 Assert.assertNotNull("can't read file " + fileName, fileObject.getChild(fileName));
186 }
187 }
188 }
189
190
191
192
193
194
195
196 @Test
197 public void testResolveNestedFileWithoutCleanup() throws IOException {
198 final File newZipFile = createTempFile();
199 final FileSystemManager manager = VFS.getManager();
200 try (final FileObject zipFileObject = manager.resolveFile("zip:file:" + newZipFile.getAbsolutePath())) {
201 @SuppressWarnings({ "unused", "resource" })
202
203 final FileObject zipFileObject1 = zipFileObject.resolveFile(NESTED_FILE_1);
204 }
205 assertDelete(newZipFile);
206 }
207 }