Home | History | Annotate | Download | only in archivers
      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 
     27 import org.apache.commons.compress.AbstractTestCase;
     28 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
     29 import org.apache.commons.compress.utils.IOUtils;
     30 import org.junit.Test;
     31 
     32 public final class JarTestCase extends AbstractTestCase {
     33 
     34     @Test
     35     public void testJarArchiveCreation() throws Exception {
     36         final File output = new File(dir, "bla.jar");
     37 
     38         final File file1 = getFile("test1.xml");
     39         final File file2 = getFile("test2.xml");
     40 
     41         final OutputStream out = new FileOutputStream(output);
     42 
     43         final ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("jar", out);
     44 
     45         os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
     46         IOUtils.copy(new FileInputStream(file1), os);
     47         os.closeArchiveEntry();
     48 
     49         os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
     50         IOUtils.copy(new FileInputStream(file2), os);
     51         os.closeArchiveEntry();
     52 
     53         os.close();
     54     }
     55 
     56 
     57     @Test
     58     public void testJarUnarchive() throws Exception {
     59         final File input = getFile("bla.jar");
     60         final InputStream is = new FileInputStream(input);
     61         final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("jar", is);
     62 
     63         ZipArchiveEntry entry = (ZipArchiveEntry)in.getNextEntry();
     64         File o = new File(dir, entry.getName());
     65         o.getParentFile().mkdirs();
     66         OutputStream out = new FileOutputStream(o);
     67         IOUtils.copy(in, out);
     68         out.close();
     69 
     70         entry = (ZipArchiveEntry)in.getNextEntry();
     71         o = new File(dir, entry.getName());
     72         o.getParentFile().mkdirs();
     73         out = new FileOutputStream(o);
     74         IOUtils.copy(in, out);
     75         out.close();
     76 
     77         entry = (ZipArchiveEntry)in.getNextEntry();
     78         o = new File(dir, entry.getName());
     79         o.getParentFile().mkdirs();
     80         out = new FileOutputStream(o);
     81         IOUtils.copy(in, out);
     82         out.close();
     83 
     84         in.close();
     85         is.close();
     86     }
     87 
     88     @Test
     89     public void testJarUnarchiveAll() throws Exception {
     90         final File input = getFile("bla.jar");
     91         final InputStream is = new FileInputStream(input);
     92         final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream("jar", is);
     93 
     94         ArchiveEntry entry = in.getNextEntry();
     95         while (entry != null) {
     96             final File archiveEntry = new File(dir, entry.getName());
     97             archiveEntry.getParentFile().mkdirs();
     98             if(entry.isDirectory()){
     99                 archiveEntry.mkdir();
    100                 entry = in.getNextEntry();
    101                 continue;
    102             }
    103             final OutputStream out = new FileOutputStream(archiveEntry);
    104             IOUtils.copy(in, out);
    105             out.close();
    106             entry = in.getNextEntry();
    107         }
    108 
    109         in.close();
    110         is.close();
    111     }
    112 
    113 }
    114