/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.apache.commons.compress.archivers; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.commons.compress.utils.IOUtils; /** * Simple command line application that can create an archive from the contents of a directory. * *
The name of the directory must be given the first command line argument.
*The second argument defines the archive type.
*The third argument is the archive file name.
* * @since 1.4.1 */ public final class Archiver { private static final ArchiveStreamFactory factory = new ArchiveStreamFactory(); private static long fileCount=0; private static long dirCount=0; public static void main(String[] args) throws Exception { if (args.length != 3) { usage(); return; } File f = new File(args[0]); if (!f.isDirectory()) { System.err.println(f + " doesn't exist or is not a directory"); return; } ArchiveOutputStream aos = factory.createArchiveOutputStream(args[1], new FileOutputStream(new File(args[2]))); writeArchive(new File(""), f, aos); aos.close(); System.out.println("Files: "+fileCount+" Folders: "+dirCount+ " Total: "+fileCount+dirCount); } private static void writeArchive(File parent, File f, ArchiveOutputStream aos) throws IOException { for(File e : f.listFiles()) { File n = new File(parent, e.getName()); aos.putArchiveEntry(aos.createArchiveEntry(e, n.getPath())); if (e.isDirectory()) { aos.closeArchiveEntry(); dirCount++; writeArchive(new File(parent,e.getName()), e, aos); } else { IOUtils.copy(new FileInputStream(e), aos); aos.closeArchiveEntry(); fileCount++; } } } private static void usage() { System.out.println("Parameters: input-directory archive-type output-archive "); } }