Torsten Curdt | 70c8320 | 2009-01-12 11:09:21 +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; |
| 24 | import java.io.InputStream; |
| 25 | import java.io.OutputStream; |
Stefan Bodewig | 004124a | 2009-03-26 10:36:45 +0000 | [diff] [blame] | 26 | import java.util.HashMap; |
| 27 | import java.util.Map; |
Torsten Curdt | 70c8320 | 2009-01-12 11:09:21 +0000 | [diff] [blame] | 28 | |
| 29 | import org.apache.commons.compress.AbstractTestCase; |
| 30 | import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry; |
| 31 | import org.apache.commons.compress.utils.IOUtils; |
| 32 | |
| 33 | public final class CpioTestCase extends AbstractTestCase { |
| 34 | |
Stefan Bodewig | 004124a | 2009-03-26 10:36:45 +0000 | [diff] [blame] | 35 | public void testCpioArchiveCreation() throws Exception { |
| 36 | final File output = new File(dir, "bla.cpio"); |
Torsten Curdt | 70c8320 | 2009-01-12 11:09:21 +0000 | [diff] [blame] | 37 | |
Stefan Bodewig | 004124a | 2009-03-26 10:36:45 +0000 | [diff] [blame] | 38 | final File file1 = getFile("test1.xml"); |
| 39 | final File file2 = getFile("test2.xml"); |
| 40 | |
| 41 | final OutputStream out = new FileOutputStream(output); |
| 42 | final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("cpio", out); |
| 43 | os.putArchiveEntry(new CpioArchiveEntry("test1.xml", file1.length())); |
| 44 | IOUtils.copy(new FileInputStream(file1), os); |
| 45 | os.closeArchiveEntry(); |
| 46 | |
| 47 | os.putArchiveEntry(new CpioArchiveEntry("test2.xml", file2.length())); |
| 48 | IOUtils.copy(new FileInputStream(file2), os); |
| 49 | os.closeArchiveEntry(); |
| 50 | |
| 51 | os.close(); |
Sebastian Bazley | e787a37 | 2009-03-26 17:50:00 +0000 | [diff] [blame^] | 52 | out.close(); |
Stefan Bodewig | 004124a | 2009-03-26 10:36:45 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public void testCpioUnarchive() throws Exception { |
| 56 | final File output = new File(dir, "bla.cpio"); |
| 57 | { |
| 58 | final File file1 = getFile("test1.xml"); |
| 59 | final File file2 = getFile("test2.xml"); |
| 60 | |
| 61 | final OutputStream out = new FileOutputStream(output); |
| 62 | final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("cpio", out); |
| 63 | os.putArchiveEntry(new CpioArchiveEntry("test1.xml", file1.length())); |
| 64 | IOUtils.copy(new FileInputStream(file1), os); |
| 65 | os.closeArchiveEntry(); |
| 66 | |
| 67 | os.putArchiveEntry(new CpioArchiveEntry("test2.xml", file2.length())); |
| 68 | IOUtils.copy(new FileInputStream(file2), os); |
| 69 | os.closeArchiveEntry(); |
| 70 | |
| 71 | os.close(); |
| 72 | out.close(); |
| 73 | } |
| 74 | |
| 75 | // Unarchive Operation |
| 76 | final File input = output; |
| 77 | final InputStream is = new FileInputStream(input); |
| 78 | final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("cpio", is); |
| 79 | |
| 80 | |
| 81 | Map result = new HashMap(); |
| 82 | ArchiveEntry entry = null; |
| 83 | while ((entry = in.getNextEntry()) != null) { |
| 84 | File target = new File(dir, entry.getName()); |
| 85 | final OutputStream out = new FileOutputStream(target); |
| 86 | IOUtils.copy(in, out); |
| 87 | out.close(); |
| 88 | result.put(entry.getName(), target); |
| 89 | } |
Torsten Curdt | 70c8320 | 2009-01-12 11:09:21 +0000 | [diff] [blame] | 90 | in.close(); |
Sebastian Bazley | e787a37 | 2009-03-26 17:50:00 +0000 | [diff] [blame^] | 91 | is.close(); |
Stefan Bodewig | 004124a | 2009-03-26 10:36:45 +0000 | [diff] [blame] | 92 | |
| 93 | int lineSepLength = System.getProperty("line.separator").length(); |
| 94 | |
| 95 | File t = (File)result.get("test1.xml"); |
| 96 | assertTrue("Expected " + t.getAbsolutePath() + " to exist", t.exists()); |
| 97 | assertEquals("length of " + t.getAbsolutePath(), |
| 98 | 72 + 4 * lineSepLength, t.length()); |
| 99 | |
| 100 | t = (File)result.get("test2.xml"); |
| 101 | assertTrue("Expected " + t.getAbsolutePath() + " to exist", t.exists()); |
| 102 | assertEquals("length of " + t.getAbsolutePath(), |
| 103 | 73 + 5 * lineSepLength, t.length()); |
| 104 | } |
Torsten Curdt | 70c8320 | 2009-01-12 11:09:21 +0000 | [diff] [blame] | 105 | |
| 106 | } |