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
027    private enum Mode {
028        LIST("Analysing") {
029            @Override
030            public void takeAction(final SevenZFile archive, final SevenZArchiveEntry entry) {
031                System.out.print(entry.getName());
032                if (entry.isDirectory()) {
033                    System.out.print(" dir");
034                } else {
035                    System.out.print(" " + entry.getCompressedSize()
036                                     + "/" + entry.getSize());
037                }
038                if (entry.getHasLastModifiedDate()) {
039                    System.out.print(" " + entry.getLastModifiedDate());
040                } else {
041                    System.out.print(" no last modified date");
042                }
043                if (!entry.isDirectory()) {
044                    System.out.println(" " + getContentMethods(entry));
045                } else {
046                    System.out.println("");
047                }
048            }
049
050            private String getContentMethods(final SevenZArchiveEntry entry) {
051                final StringBuilder sb = new StringBuilder();
052                boolean first = true;
053                for (final SevenZMethodConfiguration m : entry.getContentMethods()) {
054                    if (!first) {
055                        sb.append(", ");
056                    }
057                    first = false;
058                    sb.append(m.getMethod());
059                    if (m.getOptions() != null) {
060                        sb.append("(").append(m.getOptions()).append(")");
061                    }
062                }
063                return sb.toString();
064            }
065        },
066        EXTRACT("Extracting") {
067            private final byte[] buf = new byte[8192];
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        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}