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                final FileOutputStream fos = new FileOutputStream(outFile);
086                try {
087                    final long total = entry.getSize();
088                    long off = 0;
089                    while (off < total) {
090                        final int toRead = (int) Math.min(total - off, BUF.length);
091                        final int bytesRead = archive.read(BUF, 0, toRead);
092                        if (bytesRead < 1) {
093                            throw new IOException("reached end of entry "
094                                                  + entry.getName()
095                                                  + " after " + off
096                                                  + " bytes, expected "
097                                                  + total);
098                        }
099                        off += bytesRead;
100                        fos.write(BUF, 0, bytesRead);
101                    }
102                } finally {
103                    fos.close();
104                }
105            }
106        };
107
108        private final String message;
109        private Mode(final String message) {
110            this.message = message;
111        }
112        public String getMessage() {
113            return message;
114        }
115        public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry)
116            throws IOException;
117    }        
118
119    public static void main(final String[] args) throws Exception {
120        if (args.length == 0) {
121            usage();
122            return;
123        }
124        final Mode mode = grabMode(args);
125        System.out.println(mode.getMessage() + " " + args[0]);
126        final File f = new File(args[0]);
127        if (!f.isFile()) {
128            System.err.println(f + " doesn't exist or is a directory");
129        }
130        final SevenZFile archive = new SevenZFile(f);
131        try {
132            SevenZArchiveEntry ae;
133            while((ae=archive.getNextEntry()) != null) {
134                mode.takeAction(archive, ae);
135            }
136        } finally {
137            archive.close();
138        }
139    }
140
141    private static void usage() {
142        System.out.println("Parameters: archive-name [list|extract]");
143    }
144
145    private static Mode grabMode(final String[] args) {
146        if (args.length < 2) {
147            return Mode.LIST;
148        }
149        return Enum.valueOf(Mode.class, args[1].toUpperCase());
150    }
151
152}