Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | package org.apache.commons.compress.archivers; |
| 20 | |
| 21 | import java.io.File; |
| 22 | import java.io.FileInputStream; |
| 23 | import java.io.FileOutputStream; |
Torsten Curdt | d1789e6 | 2009-01-07 12:52:42 +0000 | [diff] [blame] | 24 | import java.io.IOException; |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 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.utils.IOUtils; |
| 31 | |
| 32 | public final class TarTestCase extends AbstractTestCase { |
| 33 | public void testTarArchiveCreation() throws Exception { |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 34 | final File output = new File(dir, "bla.tar"); |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 35 | final File file1 = new File(getClass().getClassLoader().getResource("test1.xml").getFile()); |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 36 | final OutputStream out = new FileOutputStream(output); |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 37 | final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar", out); |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 38 | final TarArchiveEntry entry = new TarArchiveEntry("testdata/test1.xml"); |
| 39 | entry.setModTime(0); |
| 40 | entry.setSize(file1.length()); |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame^] | 41 | entry.setUserId(0); |
| 42 | entry.setGroupId(0); |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 43 | entry.setUserName("avalon"); |
| 44 | entry.setGroupName("excalibur"); |
| 45 | entry.setMode(0100000); |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 46 | os.putArchiveEntry(entry); |
| 47 | IOUtils.copy(new FileInputStream(file1), os); |
Torsten Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 48 | os.closeArchiveEntry(); |
| 49 | os.close(); |
| 50 | } |
Torsten Curdt | d1789e6 | 2009-01-07 12:52:42 +0000 | [diff] [blame] | 51 | |
| 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 Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame^] | 64 | entry.setUserId(0); |
| 65 | entry.setGroupId(0); |
Torsten Curdt | d1789e6 | 2009-01-07 12:52:42 +0000 | [diff] [blame] | 66 | 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 Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame^] | 84 | entry2.setUserId(0); |
| 85 | entry2.setGroupId(0); |
Torsten Curdt | d1789e6 | 2009-01-07 12:52:42 +0000 | [diff] [blame] | 86 | 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 Curdt | a8a9e64 | 2008-07-19 22:18:44 +0000 | [diff] [blame] | 98 | 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 | } |