1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers;
20
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27
28 import org.apache.commons.compress.AbstractTestCase;
29 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
30 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
31 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
32 import org.apache.commons.compress.utils.CharsetNames;
33 import org.apache.commons.compress.utils.IOUtils;
34
35 public final class TarTestCase extends AbstractTestCase {
36 public void testTarArchiveCreation() throws Exception {
37 final File output = new File(dir, "bla.tar");
38 final File file1 = getFile("test1.xml");
39 final OutputStream out = new FileOutputStream(output);
40 final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar", out);
41 final TarArchiveEntry entry = new TarArchiveEntry("testdata/test1.xml");
42 entry.setModTime(0);
43 entry.setSize(file1.length());
44 entry.setUserId(0);
45 entry.setGroupId(0);
46 entry.setUserName("avalon");
47 entry.setGroupName("excalibur");
48 entry.setMode(0100000);
49 os.putArchiveEntry(entry);
50 IOUtils.copy(new FileInputStream(file1), os);
51 os.closeArchiveEntry();
52 os.close();
53 }
54
55 public void testTarArchiveLongNameCreation() throws Exception {
56 String name = "testdata/12345678901234567890123456789012345678901234567890123456789012345678901234567890123456.xml";
57 byte[] bytes = name.getBytes(CharsetNames.UTF_8);
58 assertEquals(bytes.length, 99);
59
60 final File output = new File(dir, "bla.tar");
61 final File file1 = getFile("test1.xml");
62 final OutputStream out = new FileOutputStream(output);
63 final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar", out);
64 final TarArchiveEntry entry = new TarArchiveEntry(name);
65 entry.setModTime(0);
66 entry.setSize(file1.length());
67 entry.setUserId(0);
68 entry.setGroupId(0);
69 entry.setUserName("avalon");
70 entry.setGroupName("excalibur");
71 entry.setMode(0100000);
72 os.putArchiveEntry(entry);
73 FileInputStream in = new FileInputStream(file1);
74 IOUtils.copy(in, os);
75 os.closeArchiveEntry();
76 os.close();
77 out.close();
78 in.close();
79
80
81 ArchiveOutputStream os2 = null;
82 try {
83 String toLongName = "testdata/123456789012345678901234567890123456789012345678901234567890123456789012345678901234567.xml";
84 final File output2 = new File(dir, "bla.tar");
85 final OutputStream out2 = new FileOutputStream(output2);
86 os2 = new ArchiveStreamFactory().createArchiveOutputStream("tar", out2);
87 final TarArchiveEntry entry2 = new TarArchiveEntry(toLongName);
88 entry2.setModTime(0);
89 entry2.setSize(file1.length());
90 entry2.setUserId(0);
91 entry2.setGroupId(0);
92 entry2.setUserName("avalon");
93 entry2.setGroupName("excalibur");
94 entry2.setMode(0100000);
95 os2.putArchiveEntry(entry);
96 IOUtils.copy(new FileInputStream(file1), os2);
97 os2.closeArchiveEntry();
98 } catch(IOException e) {
99 assertTrue(true);
100 } finally {
101 if (os2 != null){
102 os2.close();
103 }
104 }
105 }
106
107 public void testTarUnarchive() throws Exception {
108 final File input = getFile("bla.tar");
109 final InputStream is = new FileInputStream(input);
110 final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
111 final TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry();
112 final OutputStream out = new FileOutputStream(new File(dir, entry.getName()));
113 IOUtils.copy(in, out);
114 in.close();
115 out.close();
116 }
117
118 public void testCOMPRESS114() throws Exception {
119 final File input = getFile("COMPRESS-114.tar");
120 final InputStream is = new FileInputStream(input);
121 final ArchiveInputStream in = new TarArchiveInputStream(is,
122 CharsetNames.ISO_8859_1);
123 TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry();
124 assertEquals("3\u00b1\u00b1\u00b1F06\u00b1W2345\u00b1ZB\u00b1la\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1BLA", entry.getName());
125 entry = (TarArchiveEntry)in.getNextEntry();
126 assertEquals("0302-0601-3\u00b1\u00b1\u00b1F06\u00b1W2345\u00b1ZB\u00b1la\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1BLA",entry.getName());
127 in.close();
128 }
129
130 public void testDirectoryEntryFromFile() throws Exception {
131 File[] tmp = createTempDirAndFile();
132 File archive = null;
133 TarArchiveOutputStream tos = null;
134 TarArchiveInputStream tis = null;
135 try {
136 archive = File.createTempFile("test.", ".tar", tmp[0]);
137 archive.deleteOnExit();
138 tos = new TarArchiveOutputStream(new FileOutputStream(archive));
139 long beforeArchiveWrite = tmp[0].lastModified();
140 TarArchiveEntry in = new TarArchiveEntry(tmp[0], "foo");
141 tos.putArchiveEntry(in);
142 tos.closeArchiveEntry();
143 tos.close();
144 tos = null;
145 tis = new TarArchiveInputStream(new FileInputStream(archive));
146 TarArchiveEntry out = tis.getNextTarEntry();
147 tis.close();
148 tis = null;
149 assertNotNull(out);
150 assertEquals("foo/", out.getName());
151 assertEquals(0, out.getSize());
152
153 assertEquals(beforeArchiveWrite / 1000,
154 out.getLastModifiedDate().getTime() / 1000);
155 assertTrue(out.isDirectory());
156 } finally {
157 if (tis != null) {
158 tis.close();
159 }
160 if (tos != null) {
161 tos.close();
162 }
163 tryHardToDelete(archive);
164 tryHardToDelete(tmp[1]);
165 rmdir(tmp[0]);
166 }
167 }
168
169 public void testExplicitDirectoryEntry() throws Exception {
170 File[] tmp = createTempDirAndFile();
171 File archive = null;
172 TarArchiveOutputStream tos = null;
173 TarArchiveInputStream tis = null;
174 try {
175 archive = File.createTempFile("test.", ".tar", tmp[0]);
176 archive.deleteOnExit();
177 tos = new TarArchiveOutputStream(new FileOutputStream(archive));
178 long beforeArchiveWrite = tmp[0].lastModified();
179 TarArchiveEntry in = new TarArchiveEntry("foo/");
180 in.setModTime(beforeArchiveWrite);
181 tos.putArchiveEntry(in);
182 tos.closeArchiveEntry();
183 tos.close();
184 tos = null;
185 tis = new TarArchiveInputStream(new FileInputStream(archive));
186 TarArchiveEntry out = tis.getNextTarEntry();
187 tis.close();
188 tis = null;
189 assertNotNull(out);
190 assertEquals("foo/", out.getName());
191 assertEquals(0, out.getSize());
192 assertEquals(beforeArchiveWrite / 1000,
193 out.getLastModifiedDate().getTime() / 1000);
194 assertTrue(out.isDirectory());
195 } finally {
196 if (tis != null) {
197 tis.close();
198 }
199 if (tos != null) {
200 tos.close();
201 }
202 tryHardToDelete(archive);
203 tryHardToDelete(tmp[1]);
204 rmdir(tmp[0]);
205 }
206 }
207
208 public void testFileEntryFromFile() throws Exception {
209 File[] tmp = createTempDirAndFile();
210 File archive = null;
211 TarArchiveOutputStream tos = null;
212 TarArchiveInputStream tis = null;
213 FileInputStream fis = null;
214 try {
215 archive = File.createTempFile("test.", ".tar", tmp[0]);
216 archive.deleteOnExit();
217 tos = new TarArchiveOutputStream(new FileOutputStream(archive));
218 TarArchiveEntry in = new TarArchiveEntry(tmp[1], "foo");
219 tos.putArchiveEntry(in);
220 byte[] b = new byte[(int) tmp[1].length()];
221 fis = new FileInputStream(tmp[1]);
222 while (fis.read(b) > 0) {
223 tos.write(b);
224 }
225 fis.close();
226 fis = null;
227 tos.closeArchiveEntry();
228 tos.close();
229 tos = null;
230 tis = new TarArchiveInputStream(new FileInputStream(archive));
231 TarArchiveEntry out = tis.getNextTarEntry();
232 tis.close();
233 tis = null;
234 assertNotNull(out);
235 assertEquals("foo", out.getName());
236 assertEquals(tmp[1].length(), out.getSize());
237 assertEquals(tmp[1].lastModified() / 1000,
238 out.getLastModifiedDate().getTime() / 1000);
239 assertFalse(out.isDirectory());
240 } finally {
241 if (tis != null) {
242 tis.close();
243 }
244 if (tos != null) {
245 tos.close();
246 }
247 tryHardToDelete(archive);
248 if (fis != null) {
249 fis.close();
250 }
251 tryHardToDelete(tmp[1]);
252 rmdir(tmp[0]);
253 }
254 }
255
256 public void testExplicitFileEntry() throws Exception {
257 File[] tmp = createTempDirAndFile();
258 File archive = null;
259 TarArchiveOutputStream tos = null;
260 TarArchiveInputStream tis = null;
261 FileInputStream fis = null;
262 try {
263 archive = File.createTempFile("test.", ".tar", tmp[0]);
264 archive.deleteOnExit();
265 tos = new TarArchiveOutputStream(new FileOutputStream(archive));
266 TarArchiveEntry in = new TarArchiveEntry("foo");
267 in.setModTime(tmp[1].lastModified());
268 in.setSize(tmp[1].length());
269 tos.putArchiveEntry(in);
270 byte[] b = new byte[(int) tmp[1].length()];
271 fis = new FileInputStream(tmp[1]);
272 while (fis.read(b) > 0) {
273 tos.write(b);
274 }
275 fis.close();
276 fis = null;
277 tos.closeArchiveEntry();
278 tos.close();
279 tos = null;
280 tis = new TarArchiveInputStream(new FileInputStream(archive));
281 TarArchiveEntry out = tis.getNextTarEntry();
282 tis.close();
283 tis = null;
284 assertNotNull(out);
285 assertEquals("foo", out.getName());
286 assertEquals(tmp[1].length(), out.getSize());
287 assertEquals(tmp[1].lastModified() / 1000,
288 out.getLastModifiedDate().getTime() / 1000);
289 assertFalse(out.isDirectory());
290 } finally {
291 if (tis != null) {
292 tis.close();
293 }
294 if (tos != null) {
295 tos.close();
296 }
297 tryHardToDelete(archive);
298 if (fis != null) {
299 fis.close();
300 }
301 tryHardToDelete(tmp[1]);
302 rmdir(tmp[0]);
303 }
304 }
305
306 public void testCOMPRESS178() throws Exception {
307 final File input = getFile("COMPRESS-178.tar");
308 final InputStream is = new FileInputStream(input);
309 final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
310 try {
311 in.getNextEntry();
312 fail("Expected IOException");
313 } catch (IOException e) {
314 Throwable t = e.getCause();
315 assertTrue("Expected cause = IllegalArgumentException", t instanceof IllegalArgumentException);
316 }
317 in.close();
318 }
319
320 }