View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.common.tar;
21  
22  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
23  import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
24  import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
25  import org.apache.commons.compress.utils.IOUtils;
26  import org.waarp.common.file.FileUtils;
27  import org.waarp.common.logging.SysErrLogger;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.FileNotFoundException;
32  import java.io.FileOutputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  import java.io.OutputStream;
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  /**
40   *
41   */
42  public final class ZipUtility {
43  
44    private static final File[] FILE_0_LENGTH = {};
45  
46    private ZipUtility() {
47    }
48  
49    /**
50     * Create a new Zip from a root directory
51     *
52     * @param directory the base directory
53     * @param filename the output filename
54     * @param absolute store absolute filepath (from directory) or only
55     *     filename
56     *
57     * @return True if OK
58     */
59    public static boolean createZipFromDirectory(final String directory,
60                                                 final String filename,
61                                                 final boolean absolute) {
62      final File rootDir = new File(directory);
63      final File saveFile = new File(filename);
64      // recursive call
65      final ZipArchiveOutputStream zaos;
66      try {
67        zaos = new ZipArchiveOutputStream(new FileOutputStream(saveFile));
68      } catch (final FileNotFoundException e) {
69        return false;
70      }
71      try {
72        recurseFiles(rootDir, rootDir, zaos, absolute);
73      } catch (final IOException e2) {
74        FileUtils.close(zaos);
75        return false;
76      }
77      try {
78        zaos.finish();
79      } catch (final IOException e1) {
80        // ignore
81      }
82      FileUtils.close(zaos);
83      return true;
84    }
85  
86    /**
87     * Recursive traversal to add files
88     *
89     * @param root
90     * @param file
91     * @param zaos
92     * @param absolute
93     *
94     * @throws IOException
95     */
96    private static void recurseFiles(final File root, final File file,
97                                     final ZipArchiveOutputStream zaos,
98                                     final boolean absolute) throws IOException {
99      if (file.isDirectory()) {
100       // recursive call
101       final File[] files = file.listFiles();
102       for (final File file2 : files) {
103         recurseFiles(root, file2, zaos, absolute);
104       }
105     } else if (!file.getName().endsWith(".zip") &&
106                !file.getName().endsWith(".ZIP")) {
107       final String filename;
108       if (absolute) {
109         filename =
110             file.getAbsolutePath().substring(root.getAbsolutePath().length());
111       } else {
112         filename = file.getName();
113       }
114       final ZipArchiveEntry zae = new ZipArchiveEntry(filename);
115       zae.setSize(file.length());
116       zaos.putArchiveEntry(zae);
117       final FileInputStream fis = new FileInputStream(file);
118       IOUtils.copy(fis, zaos);
119       zaos.closeArchiveEntry();
120     }
121   }
122 
123   /**
124    * Create a new Zip from a list of Files (only name of files will be used)
125    *
126    * @param files list of files to add
127    * @param filename the output filename
128    *
129    * @return True if OK
130    */
131   public static boolean createZipFromFiles(final List<File> files,
132                                            final String filename) {
133     return createZipFromFiles(files.toArray(FILE_0_LENGTH), filename);
134   }
135 
136   /**
137    * Create a new Zip from an array of Files (only name of files will be used)
138    *
139    * @param files array of files to add
140    * @param filename the output filename
141    *
142    * @return True if OK
143    */
144   public static boolean createZipFromFiles(final File[] files,
145                                            final String filename) {
146     final File saveFile = new File(filename);
147     final ZipArchiveOutputStream zaos;
148     try {
149       zaos = new ZipArchiveOutputStream(new FileOutputStream(saveFile));
150     } catch (final FileNotFoundException e) {
151       return false;
152     }
153     for (final File file : files) {
154       try {
155         addFile(file, zaos);
156       } catch (final IOException e) {
157         FileUtils.close(zaos);
158         return false;
159       }
160     }
161     try {
162       zaos.finish();
163     } catch (final IOException e1) {
164       // ignore
165     }
166     FileUtils.close(zaos);
167     return true;
168   }
169 
170   /**
171    * Recursive traversal to add files
172    *
173    * @param file
174    * @param zaos
175    *
176    * @throws IOException
177    */
178   private static void addFile(final File file,
179                               final ZipArchiveOutputStream zaos)
180       throws IOException {
181     final String filename;
182     filename = file.getName();
183     final ZipArchiveEntry zae = new ZipArchiveEntry(filename);
184     zae.setSize(file.length());
185     zaos.putArchiveEntry(zae);
186     final FileInputStream fis = new FileInputStream(file);
187     IOUtils.copy(fis, zaos);
188     zaos.closeArchiveEntry();
189   }
190 
191   /**
192    * Extract all files from Tar into the specified directory
193    *
194    * @param tarFile
195    * @param directory
196    *
197    * @return the list of extracted filenames
198    *
199    * @throws IOException
200    */
201   public static List<String> unZip(final File tarFile, final File directory)
202       throws IOException {
203     final List<String> result = new ArrayList<String>();
204     final InputStream inputStream = new FileInputStream(tarFile);
205     final ZipArchiveInputStream in = new ZipArchiveInputStream(inputStream);
206     try {
207       ZipArchiveEntry entry = in.getNextZipEntry();
208       while (entry != null) {
209         if (entry.isDirectory()) {
210           entry = in.getNextZipEntry();
211           continue;
212         }
213         final File curfile = new File(directory, entry.getName());
214         final File parent = curfile.getParentFile();
215         if (!parent.exists()) {
216           parent.mkdirs();//NOSONAR
217         }
218         final OutputStream out = new FileOutputStream(curfile);
219         try {
220           IOUtils.copy(in, out);
221         } finally {
222           FileUtils.close(out);
223         }
224         result.add(entry.getName());
225         entry = in.getNextZipEntry();
226       }
227     } finally {
228       FileUtils.close(in);
229     }
230     return result;
231   }
232 
233   public static void main(final String[] args) {
234     if (args.length < 3) {
235       SysErrLogger.FAKE_LOGGER.syserr("You need to provide 3 arguments:\n" +
236                                       "   option filedest.tar \"source\"\n" +
237                                       "   where option=1 means unzip and source is a directory\n" +
238                                       "   option=2 means zip and source is a directory\n" +
239                                       "   option=3 means zip and source is a list of files comma separated");
240       System.exit(1);//NOSONAR
241     }
242     final int option = Integer.parseInt(args[0]);
243     final String tarfile = args[1];
244     final String tarsource = args[2];
245     final String[] tarfiles;
246     if (option == 3) {
247       tarfiles = args[2].split(",");
248       final File[] files = new File[tarfiles.length];
249       for (int i = 0; i < tarfiles.length; i++) {
250         files[i] = new File(tarfiles[i]);
251       }
252       if (createZipFromFiles(files, tarfile)) {
253         SysErrLogger.FAKE_LOGGER.sysout("ZIP OK from multiple files");
254       } else {
255         SysErrLogger.FAKE_LOGGER.syserr("ZIP KO from multiple files");
256       }
257     } else if (option == 2) {
258       if (createZipFromDirectory(tarsource, tarfile, false)) {
259         SysErrLogger.FAKE_LOGGER.sysout("ZIP OK from directory");
260       } else {
261         SysErrLogger.FAKE_LOGGER.syserr("ZIP KO from directory");
262       }
263     } else if (option == 1) {
264       final File tarFile = new File(tarfile);
265       final File directory = new File(tarsource);
266       List<String> result = null;
267       try {
268         result = unZip(tarFile, directory);
269       } catch (final IOException e) {
270         SysErrLogger.FAKE_LOGGER.syserr(e);
271       }
272       if (result == null || result.isEmpty()) {
273         SysErrLogger.FAKE_LOGGER.syserr("UNZIP KO from directory");
274       } else {
275         for (final String string : result) {
276           SysErrLogger.FAKE_LOGGER.sysout("File: " + string);
277         }
278       }
279     }
280 
281   }
282 }