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  import de.tivsource.page.dao.property.PropertyDaoLocal;
17  
18  /**
19   * @author Marc Michele
20   *
21   */
22  public class BackupCss {
23  
24      /**
25       * Statischer Logger der Klasse.
26       */
27      private static final Logger LOGGER = LogManager.getLogger(BackupCss.class);
28  
29      private static byte[] buffer = new byte[1024];
30  
31      private static PropertyDaoLocal propertyDaoLocal;
32  
33      public static void setPropertyDaoLocal(PropertyDaoLocal propertyDaoLocal) {
34  		BackupCss.propertyDaoLocal = propertyDaoLocal;
35  	}
36  
37  	public static File getZipFile() throws IOException {
38          LOGGER.info("getZipFile() aufgerufen.");
39      	String pathToCss = propertyDaoLocal.findByKey("css-path").getValue();
40          
41          // Zip-Datei erstellen und Stream bereitstellen.
42          File zipFile = File.createTempFile("css_tivpage", "zip");
43          ZipOutputStream outZipFile = new ZipOutputStream(new FileOutputStream(zipFile));
44  
45          File folder = new File(pathToCss);
46          File[] listOfFiles = folder.listFiles();
47          LOGGER.debug("Anzahl der Dateien: " + listOfFiles.length);
48          for (int i = 0; i < listOfFiles.length; i++) {
49              if (listOfFiles[i].isFile()) {
50                  LOGGER.debug("Füge die Datei " + listOfFiles[i].getName() + " zur Zip-Datei hinzu.");
51                  addData(listOfFiles[i], outZipFile, listOfFiles[i].getName());
52              } 
53          }
54  
55          // Schließe die Zip-Datei.
56          outZipFile.close();
57  
58          return zipFile;
59      }
60  
61      private static void addData(File file, ZipOutputStream zipOutputStream,
62              String filename) throws IOException {
63  
64          FileInputStream fileInputStream = new FileInputStream(file);
65  
66          // Ab hier beginnt der Exhibition Teil
67          zipOutputStream.putNextEntry(new ZipEntry(filename));
68  
69          // Transfer bytes from the file to the ZIP file
70          int len;
71          while ((len = fileInputStream.read(buffer)) > 0) {
72              zipOutputStream.write(buffer, 0, len);
73          }
74  
75          zipOutputStream.closeEntry();
76          fileInputStream.close();
77      }
78  
79      
80      
81  }// Ende class