View Javadoc

1   /**
2    * 
3    */
4   package de.tivsource.page.admin.backup;
5   
6   import java.io.File;
7   import java.io.FileInputStream;
8   import java.io.FileOutputStream;
9   import java.io.IOException;
10  import java.util.zip.ZipEntry;
11  import java.util.zip.ZipOutputStream;
12  
13  import org.apache.logging.log4j.LogManager;
14  import org.apache.logging.log4j.Logger;
15  
16  /**
17   * @author Marc Michele
18   *
19   */
20  public class BackupFiles {
21  
22      /**
23       * Statischer Logger der Klasse.
24       */
25      private static final Logger LOGGER = LogManager.getLogger(BackupFiles.class);
26  
27      private static byte[] buffer = new byte[1024];
28  
29      public static File getZipFile() throws IOException {
30          LOGGER.info("getZipFile() aufgerufen.");
31  
32          // Zip-Datei erstellen und Stream bereitstellen.
33          File zipFile = File.createTempFile("complete_tivpage", "zip");
34          ZipOutputStream outZipFile = new ZipOutputStream(new FileOutputStream(zipFile));
35  
36          File folder = new File("/var/www/html/uploads/");
37          File[] listOfFiles = folder.listFiles();
38          LOGGER.debug("Anzahl der Dateien: " + listOfFiles.length);
39          for (int i = 0; i < listOfFiles.length; i++) {
40              if (listOfFiles[i].isFile()) {
41                  LOGGER.debug("Füge die Datei " + listOfFiles[i].getName() + " zur Zip-Datei hinzu.");
42                  addData(listOfFiles[i], outZipFile, listOfFiles[i].getName());
43              } 
44          }
45  
46          // Schließe die Zip-Datei.
47          outZipFile.close();
48  
49          return zipFile;
50      }
51  
52      private static void addData(File file, ZipOutputStream zipOutputStream,
53              String filename) throws IOException {
54  
55          FileInputStream fileInputStream = new FileInputStream(file);
56  
57          // Ab hier beginnt der Exhibition Teil
58          zipOutputStream.putNextEntry(new ZipEntry(filename));
59  
60          // Transfer bytes from the file to the ZIP file
61          int len;
62          while ((len = fileInputStream.read(buffer)) > 0) {
63              zipOutputStream.write(buffer, 0, len);
64          }
65  
66          zipOutputStream.closeEntry();
67          fileInputStream.close();
68      }
69  
70      
71      
72  }// Ende class