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.cpio;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.InputStream;
25  
26  import org.apache.commons.compress.AbstractTestCase;
27  import org.apache.commons.compress.utils.IOUtils;
28  
29  public class CpioArchiveOutputStreamTest extends AbstractTestCase {
30  
31      public void testWriteOldBinary() throws Exception {
32          final File f = getFile("test1.xml");
33          final File output = new File(dir, "test.cpio");
34          final FileOutputStream out = new FileOutputStream(output);
35          InputStream in = null;
36          try {
37              final CpioArchiveOutputStream os =
38                  new CpioArchiveOutputStream(out, CpioConstants
39                                              .FORMAT_OLD_BINARY);
40              os.putArchiveEntry(new CpioArchiveEntry(CpioConstants
41                                                      .FORMAT_OLD_BINARY,
42                                                      f, "test1.xml"));
43              IOUtils.copy(in = new FileInputStream(f), os);
44              in.close();
45              in = null;
46              os.closeArchiveEntry();
47              os.close();
48          } finally {
49              if (in != null) {
50                  in.close();
51              }
52              out.close();
53          }
54  
55          try {
56              in = new CpioArchiveInputStream(new FileInputStream(output));
57              CpioArchiveEntry e = ((CpioArchiveInputStream) in)
58                  .getNextCPIOEntry();
59              assertEquals("test1.xml", e.getName());
60              assertNull(((CpioArchiveInputStream) in).getNextEntry());
61          } finally {
62              if (in != null) {
63                  in.close();
64              }
65          }
66      }
67  }