| Line | Hits | Source |
|---|---|---|
| 1 | /******************************************************************************* | |
| 2 | * InternetCafe is a software solution that helps the management of Cybercafes | |
| 3 | * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005. | |
| 4 | * Copyright (C) 2006 Guido Angelo Ingenito | |
| 5 | ||
| 6 | * This program is free software; you can redistribute it and/or | |
| 7 | * modify it under the terms of the GNU General Public License | |
| 8 | * as published by the Free Software Foundation; either version 2 | |
| 9 | * of the License, or (at your option) any later version. | |
| 10 | ||
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
| 19 | *******************************************************************************/ | |
| 20 | package base.backup; | |
| 21 | ||
| 22 | import java.io.BufferedInputStream; | |
| 23 | import java.io.BufferedOutputStream; | |
| 24 | import java.io.File; | |
| 25 | import java.io.FileInputStream; | |
| 26 | import java.io.FileOutputStream; | |
| 27 | import java.io.IOException; | |
| 28 | import java.io.InputStream; | |
| 29 | import java.io.PrintStream; | |
| 30 | import java.util.Date; | |
| 31 | import java.util.Enumeration; | |
| 32 | import java.util.zip.ZipEntry; | |
| 33 | import java.util.zip.ZipFile; | |
| 34 | import java.util.zip.ZipInputStream; | |
| 35 | import java.util.zip.ZipOutputStream; | |
| 36 | ||
| 37 | import javax.xml.parsers.DocumentBuilder; | |
| 38 | import javax.xml.parsers.DocumentBuilderFactory; | |
| 39 | import javax.xml.transform.Transformer; | |
| 40 | import javax.xml.transform.TransformerFactory; | |
| 41 | import javax.xml.transform.dom.DOMSource; | |
| 42 | import javax.xml.transform.stream.StreamResult; | |
| 43 | ||
| 44 | import org.apache.log4j.Logger; | |
| 45 | import org.w3c.dom.Document; | |
| 46 | ||
| 47 | import base.ConfigurationManager; | |
| 48 | import base.IdManager; | |
| 49 | ||
| 50 | 0 | public class BackupFactory { |
| 51 | 0 | private static final transient Logger logger = Logger |
| 52 | 0 | .getLogger(BackupFactory.class.getName()); |
| 53 | 0 | |
| 54 | public static Backup newBackup(String name, String description, Date date, | |
| 55 | String dbLocationPath, String zipLocationPath) { | |
| 56 | 0 | int id = IdManager.getInstance().getNextBackupId(); |
| 57 | 0 | |
| 58 | 0 | return new Backup(id, name, description, date, dbLocationPath, |
| 59 | 0 | zipLocationPath); |
| 60 | } | |
| 61 | ||
| 62 | public static Backup newBackupFromZipArchive(File backupFile) | |
| 63 | throws Exception { | |
| 64 | 0 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 65 | 0 | |
| 66 | 0 | factory.setIgnoringComments(true); |
| 67 | 0 | factory.setValidating(false); |
| 68 | 0 | factory.setIgnoringElementContentWhitespace(true); |
| 69 | 0 | |
| 70 | 0 | DocumentBuilder docBuilder = factory.newDocumentBuilder(); |
| 71 | 0 | File unzipFolder = new File(backupFile.getParent(), backupFile |
| 72 | 0 | .getName().substring(0, backupFile.getName().lastIndexOf("."))); |
| 73 | ||
| 74 | 0 | unzipFolder.mkdir(); |
| 75 | 0 | unZipFile(new ZipFile(backupFile), unzipFolder); |
| 76 | 0 | |
| 77 | 0 | // Now we must retrieve the details file |
| 78 | 0 | File xmlDetailsFile = new File(unzipFolder, |
| 79 | 0 | ConfigurationManager.BACKUP_DETAILS_FILE); |
| 80 | 0 | Document document = docBuilder |
| 81 | 0 | .parse(new FileInputStream(xmlDetailsFile)); |
| 82 | 0 | Backup backup = new Backup(document); |
| 83 | 0 | |
| 84 | 0 | backup.setZipLocationPath(backupFile.getAbsolutePath()); |
| 85 | 0 | |
| 86 | 0 | return backup; |
| 87 | 0 | } |
| 88 | 0 | |
| 89 | 0 | public static File createDetailsFile(Backup backup) throws Exception { |
| 90 | 0 | Document doc = DocumentBuilderFactory.newInstance() |
| 91 | 0 | .newDocumentBuilder().newDocument(); |
| 92 | 0 | |
| 93 | 0 | doc.appendChild(backup.toXml(doc)); |
| 94 | 0 | |
| 95 | 0 | String detailsfileName = ConfigurationManager.BACKUP_DETAILS_FILE; |
| 96 | 0 | |
| 97 | 0 | if (!detailsfileName.endsWith(".xml")) { |
| 98 | 0 | detailsfileName += ".xml"; |
| 99 | 0 | } |
| 100 | 0 | |
| 101 | 0 | Transformer transformer = TransformerFactory.newInstance() |
| 102 | .newTransformer(); | |
| 103 | 0 | DOMSource source = new DOMSource(doc); |
| 104 | 0 | StreamResult streamResult = new StreamResult(new PrintStream( |
| 105 | 0 | detailsfileName)); |
| 106 | 0 | |
| 107 | 0 | transformer.transform(source, streamResult); |
| 108 | ||
| 109 | 0 | return new File(detailsfileName); |
| 110 | 0 | } |
| 111 | 0 | |
| 112 | 0 | public static void backupToZipArchive(Backup backup) throws Exception { |
| 113 | 0 | // Create the ZIP file, will be rappresented by the backup's name |
| 114 | // followed by its id. | |
| 115 | 0 | String outFilename = backup.getName() + backup.getId() + ".zip"; |
| 116 | 0 | ZipOutputStream out = new ZipOutputStream(new FileOutputStream( |
| 117 | new File(backup.getZipLocationPath(), outFilename))); | |
| 118 | 0 | |
| 119 | 0 | out.setComment(backup.getDescription()); |
| 120 | 0 | |
| 121 | // Link the db path to the current zip location | |
| 122 | 0 | String dbFileName = backup.getDbLocationPath().substring( |
| 123 | 0 | backup.getDbLocationPath().lastIndexOf(File.separatorChar), |
| 124 | 0 | backup.getDbLocationPath().length()); |
| 125 | 0 | |
| 126 | 0 | backup.setDbLocationPath(new File(backup.getZipLocationPath(), |
| 127 | 0 | dbFileName).getAbsolutePath()); |
| 128 | 0 | |
| 129 | 0 | // We must create the backup's details file (xml) |
| 130 | 0 | File details = createDetailsFile(backup); |
| 131 | ||
| 132 | 0 | // We must zip it |
| 133 | 0 | zipFile(details, out); |
| 134 | 0 | |
| 135 | 0 | // Lets delete the backup's details file (xml) it doesn't serve anymore |
| 136 | 0 | details.delete(); |
| 137 | 0 | |
| 138 | 0 | File resourcesFolder = new File( |
| 139 | 0 | ConfigurationManager.RESOURCES_DIRECTORY); |
| 140 | 0 | |
| 141 | 0 | zipDirectory(resourcesFolder, out); |
| 142 | 0 | |
| 143 | 0 | // Complete the ZIP file |
| 144 | 0 | out.close(); |
| 145 | 0 | } |
| 146 | 0 | |
| 147 | 0 | public static void zipDirectory(File file, ZipOutputStream out) |
| 148 | 0 | throws Exception { |
| 149 | 0 | String[] dirList = file.list(); |
| 150 | 0 | byte[] readBuffer = new byte[1024]; |
| 151 | 0 | int bytesIn = 0; |
| 152 | 0 | |
| 153 | 0 | for (int i = 0; i < dirList.length; i++) { |
| 154 | 0 | File f = new File(file, dirList[i]); |
| 155 | 0 | |
| 156 | 0 | if (f.isDirectory()) { |
| 157 | // if the File object is a directory, call this | |
| 158 | 0 | // function again to add its content recursively |
| 159 | 0 | zipDirectory(f, out); |
| 160 | ||
| 161 | 0 | // loop again |
| 162 | 0 | continue; |
| 163 | 0 | } |
| 164 | 0 | |
| 165 | // if we reached here, the File object f was not a directory | |
| 166 | // create a FileInputStream on top of f | |
| 167 | 0 | FileInputStream fis = new FileInputStream(f); |
| 168 | 0 | |
| 169 | 0 | // create a new zip entry |
| 170 | 0 | ZipEntry zipEntry = new ZipEntry(f.getPath()); |
| 171 | ||
| 172 | 0 | // place the zip entry in the ZipOutputStream object |
| 173 | 0 | out.putNextEntry(zipEntry); |
| 174 | ||
| 175 | 0 | // now write the content of the file to the ZipOutputStream |
| 176 | 0 | while ((bytesIn = fis.read(readBuffer)) != -1) { |
| 177 | 0 | out.write(readBuffer, 0, bytesIn); |
| 178 | 0 | } |
| 179 | 0 | |
| 180 | 0 | // close the Stream |
| 181 | 0 | fis.close(); |
| 182 | } | |
| 183 | 0 | } |
| 184 | ||
| 185 | 0 | public static void zipFile(File file, ZipOutputStream out) throws Exception { |
| 186 | 0 | // Create a buffer for reading the file |
| 187 | 0 | byte[] buf = new byte[1024]; |
| 188 | 0 | |
| 189 | 0 | out.putNextEntry(new ZipEntry(file.getName())); |
| 190 | 0 | |
| 191 | 0 | FileInputStream in = new FileInputStream(file); |
| 192 | 0 | |
| 193 | 0 | // Transfer bytes from the file to the ZIP file |
| 194 | 0 | int len; |
| 195 | 0 | |
| 196 | 0 | while ((len = in.read(buf)) > 0) { |
| 197 | 0 | out.write(buf, 0, len); |
| 198 | 0 | } |
| 199 | 0 | |
| 200 | 0 | in.close(); |
| 201 | 0 | |
| 202 | // Complete the entry | |
| 203 | 0 | out.closeEntry(); |
| 204 | 0 | } |
| 205 | 0 | |
| 206 | 0 | public static void unZipFile(ZipFile zipFile, File destinationDirectory) |
| 207 | 0 | throws Exception { |
| 208 | 0 | logger.debug("unZipFile: " + zipFile.getName() |
| 209 | 0 | + " destination directory: " + destinationDirectory); |
| 210 | 0 | |
| 211 | 0 | Enumeration enumeration = zipFile.entries(); |
| 212 | ||
| 213 | 0 | while (enumeration.hasMoreElements()) { |
| 214 | 0 | ZipEntry zipEntry = (ZipEntry) enumeration.nextElement(); |
| 215 | 0 | |
| 216 | 0 | File file = new File(destinationDirectory.getCanonicalPath(), |
| 217 | 0 | zipEntry.getName().replaceFirst(".." + File.separatorChar, |
| 218 | 0 | "")); |
| 219 | 0 | |
| 220 | 0 | logger.debug("File: " + file); |
| 221 | 0 | |
| 222 | 0 | if (zipEntry.isDirectory()) { |
| 223 | 0 | file.mkdirs(); |
| 224 | 0 | } else { |
| 225 | 0 | InputStream is = zipFile.getInputStream(zipEntry); |
| 226 | 0 | BufferedInputStream bis = new BufferedInputStream(is); |
| 227 | 0 | |
| 228 | 0 | if (file.getParent() != null) { |
| 229 | 0 | File dir = new File(file.getParent()); |
| 230 | 0 | |
| 231 | 0 | dir.mkdirs(); |
| 232 | 0 | } |
| 233 | 0 | |
| 234 | 0 | FileOutputStream fos = new FileOutputStream(file); |
| 235 | 0 | BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 236 | 0 | |
| 237 | int c; | |
| 238 | 0 | |
| 239 | 0 | while ((c = bis.read()) != -1) { |
| 240 | 0 | bos.write((byte) c); |
| 241 | 0 | } |
| 242 | 0 | |
| 243 | 0 | bos.close(); |
| 244 | 0 | fos.close(); |
| 245 | 0 | } |
| 246 | ||
| 247 | 0 | logger.debug(" unzipped."); |
| 248 | 0 | } |
| 249 | 0 | } |
| 250 | ||
| 251 | 0 | public static void unzip(ZipInputStream zin, String zipEntry, |
| 252 | 0 | File destinationFolder) throws IOException { |
| 253 | 0 | System.out |
| 254 | .println("unzipping " + zipEntry + " to " + destinationFolder); | |
| 255 | ||
| 256 | 0 | FileOutputStream out = new FileOutputStream(new File(destinationFolder, |
| 257 | new File(zipEntry).getName())); | |
| 258 | 0 | byte[] b = new byte[1024]; |
| 259 | 0 | int len = 0; |
| 260 | 0 | |
| 261 | 0 | while ((len = zin.read(b)) != -1) { |
| 262 | 0 | out.write(b, 0, len); |
| 263 | 0 | } |
| 264 | ||
| 265 | 0 | out.close(); |
| 266 | 0 | } |
| 267 | 0 | |
| 268 | public static long fileSizeInKB(File file) { | |
| 269 | 0 | long size = 0; |
| 270 | 0 | |
| 271 | 0 | if (file.isDirectory()) { |
| 272 | 0 | File[] filelist = file.listFiles(); |
| 273 | 0 | |
| 274 | 0 | for (int i = 0; i < filelist.length; i++) { |
| 275 | 0 | if (filelist[i].isDirectory()) { |
| 276 | 0 | size += fileSizeInKB(filelist[i]); |
| 277 | 0 | } else { |
| 278 | 0 | size += filelist[i].length(); |
| 279 | 0 | } |
| 280 | 0 | } |
| 281 | 0 | } else { |
| 282 | 0 | size += file.length(); |
| 283 | 0 | } |
| 284 | ||
| 285 | 0 | return size / 1024; |
| 286 | 0 | } |
| 287 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |