blob: de3ef5735bfec3c893639512e5665f917a5c97b8 [file] [log] [blame]
Torsten Curdt70c83202009-01-12 11:09:21 +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;
24import java.io.InputStream;
25import java.io.OutputStream;
Stefan Bodewig004124a2009-03-26 10:36:45 +000026import java.util.HashMap;
27import java.util.Map;
Torsten Curdt70c83202009-01-12 11:09:21 +000028
29import org.apache.commons.compress.AbstractTestCase;
30import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
31import org.apache.commons.compress.utils.IOUtils;
32
33public final class CpioTestCase extends AbstractTestCase {
34
Stefan Bodewig004124a2009-03-26 10:36:45 +000035 public void testCpioArchiveCreation() throws Exception {
36 final File output = new File(dir, "bla.cpio");
Torsten Curdt70c83202009-01-12 11:09:21 +000037
Stefan Bodewig004124a2009-03-26 10:36:45 +000038 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 Bazleye787a372009-03-26 17:50:00 +000052 out.close();
Stefan Bodewig004124a2009-03-26 10:36:45 +000053 }
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 Curdt70c83202009-01-12 11:09:21 +000090 in.close();
Sebastian Bazleye787a372009-03-26 17:50:00 +000091 is.close();
Stefan Bodewig004124a2009-03-26 10:36:45 +000092
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 Curdt70c83202009-01-12 11:09:21 +0000105
106}