View Javadoc
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;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.commons.compress.AbstractTestCase;
30  import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
31  import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;
32  import org.apache.commons.compress.archivers.cpio.CpioArchiveOutputStream;
33  import org.apache.commons.compress.archivers.cpio.CpioConstants;
34  import org.apache.commons.compress.utils.IOUtils;
35  
36  public final class CpioTestCase extends AbstractTestCase {
37  
38      public void testCpioArchiveCreation() throws Exception {
39          final File output = new File(dir, "bla.cpio");
40  
41          final File file1 = getFile("test1.xml");
42          final File file2 = getFile("test2.xml");
43  
44          final OutputStream out = new FileOutputStream(output);
45          final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("cpio", out);
46          os.putArchiveEntry(new CpioArchiveEntry("test1.xml", file1.length()));
47          IOUtils.copy(new FileInputStream(file1), os);
48          os.closeArchiveEntry();
49  
50          os.putArchiveEntry(new CpioArchiveEntry("test2.xml", file2.length()));
51          IOUtils.copy(new FileInputStream(file2), os);
52          os.closeArchiveEntry();
53  
54          os.close();
55          out.close();
56      }
57  
58      public void testCpioUnarchive() throws Exception {
59          final File output = new File(dir, "bla.cpio");
60          final File file1 = getFile("test1.xml");
61          final File file2 = getFile("test2.xml");
62          final long file1Length = file1.length();
63          final long file2Length = file2.length();
64  
65          {
66              final OutputStream out = new FileOutputStream(output);
67              final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("cpio", out);
68              CpioArchiveEntry entry = new CpioArchiveEntry("test1.xml", file1Length);
69              entry.setMode(CpioConstants.C_ISREG);
70              os.putArchiveEntry(entry);
71              IOUtils.copy(new FileInputStream(file1), os);
72              os.closeArchiveEntry();
73  
74              entry = new CpioArchiveEntry("test2.xml", file2Length);
75              entry.setMode(CpioConstants.C_ISREG);
76              os.putArchiveEntry(entry);
77              IOUtils.copy(new FileInputStream(file2), os);
78              os.closeArchiveEntry();
79              os.finish();
80              os.close();
81              out.close();
82          }
83  
84          // Unarchive Operation
85          final File input = output;
86          final InputStream is = new FileInputStream(input);
87          final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("cpio", is);
88  
89  
90          Map<String, File> result = new HashMap<String, File>();
91          ArchiveEntry entry = null;
92          while ((entry = in.getNextEntry()) != null) {
93              File cpioget = new File(dir, entry.getName());
94              final OutputStream out = new FileOutputStream(cpioget);
95              IOUtils.copy(in, out);
96              out.close();
97              result.put(entry.getName(), cpioget);
98          }
99          in.close();
100         is.close();
101 
102         File t = result.get("test1.xml");
103         assertTrue("Expected " + t.getAbsolutePath() + " to exist", t.exists());
104         assertEquals("length of " + t.getAbsolutePath(), file1Length, t.length());
105 
106         t = result.get("test2.xml");
107         assertTrue("Expected " + t.getAbsolutePath() + " to exist", t.exists());
108         assertEquals("length of " + t.getAbsolutePath(), file2Length, t.length());
109     }
110 
111     public void testDirectoryEntryFromFile() throws Exception {
112         File[] tmp = createTempDirAndFile();
113         File archive = null;
114         CpioArchiveOutputStream tos = null;
115         CpioArchiveInputStream tis = null;
116         try {
117             archive = File.createTempFile("test.", ".cpio", tmp[0]);
118             archive.deleteOnExit();
119             tos = new CpioArchiveOutputStream(new FileOutputStream(archive));
120             long beforeArchiveWrite = tmp[0].lastModified();
121             CpioArchiveEntry in = new CpioArchiveEntry(tmp[0], "foo");
122             tos.putArchiveEntry(in);
123             tos.closeArchiveEntry();
124             tos.close();
125             tos = null;
126             tis = new CpioArchiveInputStream(new FileInputStream(archive));
127             CpioArchiveEntry out = tis.getNextCPIOEntry();
128             tis.close();
129             tis = null;
130             assertNotNull(out);
131             assertEquals("foo", out.getName());
132             assertEquals(0, out.getSize());
133             // CPIO stores time with a granularity of 1 second
134             assertEquals(beforeArchiveWrite / 1000,
135                          out.getLastModifiedDate().getTime() / 1000);
136             assertTrue(out.isDirectory());
137         } finally {
138             if (tis != null) {
139                 tis.close();
140             }
141             if (tos != null) {
142                 tos.close();
143             }
144             tryHardToDelete(archive);
145             tryHardToDelete(tmp[1]);
146             rmdir(tmp[0]);
147         }
148     }
149 
150     public void testExplicitDirectoryEntry() throws Exception {
151         File[] tmp = createTempDirAndFile();
152         File archive = null;
153         CpioArchiveOutputStream tos = null;
154         CpioArchiveInputStream tis = null;
155         try {
156             archive = File.createTempFile("test.", ".cpio", tmp[0]);
157             archive.deleteOnExit();
158             tos = new CpioArchiveOutputStream(new FileOutputStream(archive));
159             long beforeArchiveWrite = tmp[0].lastModified();
160             CpioArchiveEntry in = new CpioArchiveEntry("foo/");
161             in.setTime(beforeArchiveWrite / 1000);
162             in.setMode(CpioConstants.C_ISDIR);
163             tos.putArchiveEntry(in);
164             tos.closeArchiveEntry();
165             tos.close();
166             tos = null;
167             tis = new CpioArchiveInputStream(new FileInputStream(archive));
168             CpioArchiveEntry out = tis.getNextCPIOEntry();
169             tis.close();
170             tis = null;
171             assertNotNull(out);
172             assertEquals("foo/", out.getName());
173             assertEquals(0, out.getSize());
174             assertEquals(beforeArchiveWrite / 1000,
175                          out.getLastModifiedDate().getTime() / 1000);
176             assertTrue(out.isDirectory());
177         } finally {
178             if (tis != null) {
179                 tis.close();
180             }
181             if (tos != null) {
182                 tos.close();
183             }
184             tryHardToDelete(archive);
185             tryHardToDelete(tmp[1]);
186             rmdir(tmp[0]);
187         }
188     }
189 
190     public void testFileEntryFromFile() throws Exception {
191         File[] tmp = createTempDirAndFile();
192         File archive = null;
193         CpioArchiveOutputStream tos = null;
194         CpioArchiveInputStream tis = null;
195         FileInputStream fis = null;
196         try {
197             archive = File.createTempFile("test.", ".cpio", tmp[0]);
198             archive.deleteOnExit();
199             tos = new CpioArchiveOutputStream(new FileOutputStream(archive));
200             CpioArchiveEntry in = new CpioArchiveEntry(tmp[1], "foo");
201             tos.putArchiveEntry(in);
202             byte[] b = new byte[(int) tmp[1].length()];
203             fis = new FileInputStream(tmp[1]);
204             while (fis.read(b) > 0) {
205                 tos.write(b);
206             }
207             fis.close();
208             fis = null;
209             tos.closeArchiveEntry();
210             tos.close();
211             tos = null;
212             tis = new CpioArchiveInputStream(new FileInputStream(archive));
213             CpioArchiveEntry out = tis.getNextCPIOEntry();
214             tis.close();
215             tis = null;
216             assertNotNull(out);
217             assertEquals("foo", out.getName());
218             assertEquals(tmp[1].length(), out.getSize());
219             assertEquals(tmp[1].lastModified() / 1000,
220                          out.getLastModifiedDate().getTime() / 1000);
221             assertFalse(out.isDirectory());
222         } finally {
223             if (tis != null) {
224                 tis.close();
225             }
226             if (tos != null) {
227                 tos.close();
228             }
229             tryHardToDelete(archive);
230             if (fis != null) {
231                 fis.close();
232             }
233             tryHardToDelete(tmp[1]);
234             rmdir(tmp[0]);
235         }
236     }
237 
238     public void testExplicitFileEntry() throws Exception {
239         File[] tmp = createTempDirAndFile();
240         File archive = null;
241         CpioArchiveOutputStream tos = null;
242         CpioArchiveInputStream tis = null;
243         FileInputStream fis = null;
244         try {
245             archive = File.createTempFile("test.", ".cpio", tmp[0]);
246             archive.deleteOnExit();
247             tos = new CpioArchiveOutputStream(new FileOutputStream(archive));
248             CpioArchiveEntry in = new CpioArchiveEntry("foo");
249             in.setTime(tmp[1].lastModified() / 1000);
250             in.setSize(tmp[1].length());
251             in.setMode(CpioConstants.C_ISREG);
252             tos.putArchiveEntry(in);
253             byte[] b = new byte[(int) tmp[1].length()];
254             fis = new FileInputStream(tmp[1]);
255             while (fis.read(b) > 0) {
256                 tos.write(b);
257             }
258             fis.close();
259             fis = null;
260             tos.closeArchiveEntry();
261             tos.close();
262             tos = null;
263             tis = new CpioArchiveInputStream(new FileInputStream(archive));
264             CpioArchiveEntry out = tis.getNextCPIOEntry();
265             tis.close();
266             tis = null;
267             assertNotNull(out);
268             assertEquals("foo", out.getName());
269             assertEquals(tmp[1].length(), out.getSize());
270             assertEquals(tmp[1].lastModified() / 1000,
271                          out.getLastModifiedDate().getTime() / 1000);
272             assertFalse(out.isDirectory());
273         } finally {
274             if (tis != null) {
275                 tis.close();
276             }
277             if (tos != null) {
278                 tos.close();
279             }
280             tryHardToDelete(archive);
281             if (fis != null) {
282                 fis.close();
283             }
284             tryHardToDelete(tmp[1]);
285             rmdir(tmp[0]);
286         }
287     }
288 }