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.IOException;
022import java.io.OutputStream;
023import java.nio.file.Files;
024
025public class CLI {
026
027
028    private 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            private final byte[] buf = new byte[8192];
069            @Override
070            public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry)
071                throws IOException {
072                final File outFile = new File(entry.getName());
073                if (entry.isDirectory()) {
074                    if (!outFile.isDirectory() && !outFile.mkdirs()) {
075                        throw new IOException("Cannot create directory " + outFile);
076                    }
077                    System.out.println("created directory " + outFile);
078                    return;
079                }
080
081                System.out.println("extracting to " + outFile);
082                final File parent = outFile.getParentFile();
083                if (parent != null && !parent.exists() && !parent.mkdirs()) {
084                    throw new IOException("Cannot create " + parent);
085                }
086                try (final OutputStream fos = Files.newOutputStream(outFile.toPath())) {
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                }
103            }
104        };
105
106        private final String message;
107        Mode(final String message) {
108            this.message = message;
109        }
110        public String getMessage() {
111            return message;
112        }
113        public abstract void takeAction(SevenZFile archive, SevenZArchiveEntry entry)
114            throws IOException;
115    }
116
117    public static void main(final String[] args) throws Exception {
118        if (args.length == 0) {
119            usage();
120            return;
121        }
122        final Mode mode = grabMode(args);
123        System.out.println(mode.getMessage() + " " + args[0]);
124        final File f = new File(args[0]);
125        if (!f.isFile()) {
126            System.err.println(f + " doesn't exist or is a directory");
127        }
128        try (final SevenZFile archive = new SevenZFile(f)) {
129            SevenZArchiveEntry ae;
130            while((ae=archive.getNextEntry()) != null) {
131                mode.takeAction(archive, ae);
132            }
133        }
134    }
135
136    private static void usage() {
137        System.out.println("Parameters: archive-name [list|extract]");
138    }
139
140    private static Mode grabMode(final String[] args) {
141        if (args.length < 2) {
142            return Mode.LIST;
143        }
144        return Enum.valueOf(Mode.class, args[1].toUpperCase());
145    }
146
147}