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