001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.commons.compress.archivers.sevenz; 019 020import java.io.File; 021import java.io.FileOutputStream; 022import java.io.IOException; 023 024public class CLI { 025 026 private static final byte[] BUF = new byte[8192]; 027 028 private static enum Mode { 029 LIST("Analysing") { 030 @Override 031 public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) { 032 System.out.print(entry.getName()); 033 if (entry.isDirectory()) { 034 System.out.print(" dir"); 035 } else { 036 System.out.print(" " + entry.getCompressedSize() 037 + "/" + entry.getSize()); 038 } 039 if (entry.getHasLastModifiedDate()) { 040 System.out.print(" " + entry.getLastModifiedDate()); 041 } else { 042 System.out.print(" no last modified date"); 043 } 044 if (!entry.isDirectory()) { 045 System.out.println(" " + getContentMethods(entry)); 046 } else { 047 System.out.println(""); 048 } 049 } 050 051 private String getContentMethods(final SevenZArchiveEntry entry) { 052 final StringBuilder sb = new StringBuilder(); 053 boolean first = true; 054 for (final SevenZMethodConfiguration m : entry.getContentMethods()) { 055 if (!first) { 056 sb.append(", "); 057 } 058 first = false; 059 sb.append(m.getMethod()); 060 if (m.getOptions() != null) { 061 sb.append("(").append(m.getOptions()).append(")"); 062 } 063 } 064 return sb.toString(); 065 } 066 }, 067 EXTRACT("Extracting") { 068 @Override 069 public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) 070 throws IOException { 071 final File outFile = new File(entry.getName()); 072 if (entry.isDirectory()) { 073 if (!outFile.isDirectory() && !outFile.mkdirs()) { 074 throw new IOException("Cannot create directory " + outFile); 075 } 076 System.out.println("created directory " + outFile); 077 return; 078 } 079 080 System.out.println("extracting to " + outFile); 081 final File parent = outFile.getParentFile(); 082 if (parent != null && !parent.exists() && !parent.mkdirs()) { 083 throw new IOException("Cannot create " + parent); 084 } 085 try (final FileOutputStream fos = new FileOutputStream(outFile)) { 086 final long total = entry.getSize(); 087 long off = 0; 088 while (off < total) { 089 final int toRead = (int) Math.min(total - off, BUF.length); 090 final int bytesRead = archive.read(BUF, 0, toRead); 091 if (bytesRead < 1) { 092 throw new IOException("reached end of entry " 093 + entry.getName() 094 + " after " + off 095 + " bytes, expected " 096 + total); 097 } 098 off += bytesRead; 099 fos.write(BUF, 0, bytesRead); 100 } 101 } 102 } 103 }; 104 105 private final String message; 106 private Mode(final String message) { 107 this.message = message; 108 } 109 public String getMessage() { 110 return message; 111 } 112 public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry) 113 throws IOException; 114 } 115 116 public static void main(final String[] args) throws Exception { 117 if (args.length == 0) { 118 usage(); 119 return; 120 } 121 final Mode mode = grabMode(args); 122 System.out.println(mode.getMessage() + " " + args[0]); 123 final File f = new File(args[0]); 124 if (!f.isFile()) { 125 System.err.println(f + " doesn't exist or is a directory"); 126 } 127 try (final SevenZFile archive = new SevenZFile(f)) { 128 SevenZArchiveEntry ae; 129 while((ae=archive.getNextEntry()) != null) { 130 mode.takeAction(archive, ae); 131 } 132 } 133 } 134 135 private static void usage() { 136 System.out.println("Parameters: archive-name [list|extract]"); 137 } 138 139 private static Mode grabMode(final String[] args) { 140 if (args.length < 2) { 141 return Mode.LIST; 142 } 143 return Enum.valueOf(Mode.class, args[1].toUpperCase()); 144 } 145 146}