blob: 2e379527bb9f193fa1b10fd3bb696a0dd252b757 [file] [log] [blame]
Torsten Curdta8a9e642008-07-19 22:18:44 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.commons.compress.archivers;
20
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileOutputStream;
Torsten Curdtd1789e62009-01-07 12:52:42 +000024import java.io.IOException;
Torsten Curdta8a9e642008-07-19 22:18:44 +000025import java.io.InputStream;
26import java.io.OutputStream;
27
28import org.apache.commons.compress.AbstractTestCase;
29import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
30import org.apache.commons.compress.utils.IOUtils;
31
32public final class TarTestCase extends AbstractTestCase {
33 public void testTarArchiveCreation() throws Exception {
Torsten Curdta8a9e642008-07-19 22:18:44 +000034 final File output = new File(dir, "bla.tar");
Torsten Curdta8a9e642008-07-19 22:18:44 +000035 final File file1 = new File(getClass().getClassLoader().getResource("test1.xml").getFile());
Torsten Curdta8a9e642008-07-19 22:18:44 +000036 final OutputStream out = new FileOutputStream(output);
Torsten Curdta8a9e642008-07-19 22:18:44 +000037 final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar", out);
Torsten Curdta8a9e642008-07-19 22:18:44 +000038 final TarArchiveEntry entry = new TarArchiveEntry("testdata/test1.xml");
39 entry.setModTime(0);
40 entry.setSize(file1.length());
Torsten Curdt46ad24d2009-01-08 11:09:25 +000041 entry.setUserId(0);
42 entry.setGroupId(0);
Torsten Curdta8a9e642008-07-19 22:18:44 +000043 entry.setUserName("avalon");
44 entry.setGroupName("excalibur");
45 entry.setMode(0100000);
Torsten Curdta8a9e642008-07-19 22:18:44 +000046 os.putArchiveEntry(entry);
47 IOUtils.copy(new FileInputStream(file1), os);
Torsten Curdta8a9e642008-07-19 22:18:44 +000048 os.closeArchiveEntry();
49 os.close();
50 }
Torsten Curdtd1789e62009-01-07 12:52:42 +000051
52 public void testTarArchiveLongNameCreation() throws Exception {
53 String name = "testdata/12345678901234567890123456789012345678901234567890123456789012345678901234567890123456.xml";
54 byte[] bytes = name.getBytes();
55 assertEquals(bytes.length, 99);
56
57 final File output = new File(dir, "bla.tar");
58 final File file1 = new File(getClass().getClassLoader().getResource("test1.xml").getFile());
59 final OutputStream out = new FileOutputStream(output);
60 final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar", out);
61 final TarArchiveEntry entry = new TarArchiveEntry(name);
62 entry.setModTime(0);
63 entry.setSize(file1.length());
Torsten Curdt46ad24d2009-01-08 11:09:25 +000064 entry.setUserId(0);
65 entry.setGroupId(0);
Torsten Curdtd1789e62009-01-07 12:52:42 +000066 entry.setUserName("avalon");
67 entry.setGroupName("excalibur");
68 entry.setMode(0100000);
69 os.putArchiveEntry(entry);
70 IOUtils.copy(new FileInputStream(file1), os);
71 os.closeArchiveEntry();
72 os.close();
73
74
75 ArchiveOutputStream os2 = null;
76 try {
77 String toLongName = "testdata/123456789012345678901234567890123456789012345678901234567890123456789012345678901234567.xml";
78 final File output2 = new File(dir, "bla.tar");
79 final OutputStream out2 = new FileOutputStream(output2);
80 os2 = new ArchiveStreamFactory().createArchiveOutputStream("tar", out2);
81 final TarArchiveEntry entry2 = new TarArchiveEntry(toLongName);
82 entry2.setModTime(0);
83 entry2.setSize(file1.length());
Torsten Curdt46ad24d2009-01-08 11:09:25 +000084 entry2.setUserId(0);
85 entry2.setGroupId(0);
Torsten Curdtd1789e62009-01-07 12:52:42 +000086 entry2.setUserName("avalon");
87 entry2.setGroupName("excalibur");
88 entry2.setMode(0100000);
89 os.putArchiveEntry(entry);
90 IOUtils.copy(new FileInputStream(file1), os2);
91 } catch(IOException e) {
92 assertTrue(true);
93 } finally {
94 os2.closeArchiveEntry();
95 }
96 }
97
Torsten Curdta8a9e642008-07-19 22:18:44 +000098 public void testTarUnarchive() throws Exception {
99 final File input = new File(getClass().getClassLoader().getResource("bla.tar").getFile());
100 final InputStream is = new FileInputStream(input);
101 final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("tar", is);
102 final TarArchiveEntry entry = (TarArchiveEntry)in.getNextEntry();
103 final OutputStream out = new FileOutputStream(new File(dir, entry.getName()));
104 IOUtils.copy(in, out);
105 out.close();
106 in.close();
107 }
108
109}