001: /*
002: * MCS Media Computer Software Copyright (c) 2005 by MCS
003: * -------------------------------------- Created on 16.01.2004 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * 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, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.utils;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.util.zip.ZipEntry;
023: import java.util.zip.ZipOutputStream;
024:
025: /**
026: * FolderZiper provide a static method to zip a folder.
027: *
028: * @author pitchoun
029: */
030: public final class FolderZipper {
031:
032: /** prevent instancing. */
033: private FolderZipper() {
034: }
035:
036: /**
037: * Zip the srcFolder into the destFileZipFile. All the folder subtree of the
038: * src folder is added to the destZipFile archive. TODO handle the usecase
039: * of srcFolder being en file.
040: *
041: * @param srcFolder
042: * String, the path of the srcFolder
043: * @param destZipFile
044: * String, the path of the destination zipFile. This file will be
045: * created or erased.
046: */
047: public static void zipFolder(final String srcFolder,
048: final String destZipFile) {
049: ZipOutputStream zip = null;
050: FileOutputStream fileWriter = null;
051: try {
052: fileWriter = new FileOutputStream(destZipFile);
053: zip = new ZipOutputStream(fileWriter);
054: } catch (Exception ex) {
055: ex.printStackTrace();
056: System.exit(0);
057: }
058:
059: addFolderToZip("", srcFolder, zip);
060: try {
061: zip.flush();
062: zip.close();
063: } catch (Exception ex) {
064: ex.printStackTrace();
065: }
066: }
067:
068: /**
069: * Write the content of srcFile in a new ZipEntry, named path+srcFile, of
070: * the zip stream. The result is that the srcFile will be in the path folder
071: * in the generated archive.
072: *
073: * @param path
074: * String, the relatif path with the root archive.
075: * @param srcName
076: * String, the absolute path of the file to add
077: * @param zip
078: * ZipOutputStram, the stream to use to write the given file.
079: */
080: private static void addToZip(final String path,
081: final String srcName, final ZipOutputStream zip) {
082:
083: File folder = new File(srcName);
084: if (folder.isDirectory()) {
085: if (path.equals("")) {
086: addFolderToZip(folder.getName(), srcName, zip);
087: } else {
088: addFolderToZip(path + "/" + folder.getName(), srcName,
089: zip);
090: }
091: } else {
092: // Transfer bytes from in to out
093: byte[] buf = new byte[1024];
094: int len;
095: try {
096: File srcFile = new File(srcName);
097: FileInputStream in = new FileInputStream(srcName);
098: try {
099: String zipEntryPrefix = "";
100: if (!path.equals("")) {
101: zipEntryPrefix = path + "/";
102: }
103: ZipEntry zipEntry = new ZipEntry(zipEntryPrefix
104: + folder.getName());
105: zipEntry.setTime(srcFile.lastModified());
106: zip.putNextEntry(zipEntry);
107: while ((len = in.read(buf)) > 0) {
108: zip.write(buf, 0, len);
109: }
110: } finally {
111: in.close();
112: }
113: } catch (Exception ex) {
114: ex.printStackTrace();
115: }
116: }
117: }
118:
119: /**
120: * add the srcFolder to the zip stream.
121: *
122: * @param path
123: * String, the relatif path with the root archive.
124: * @param srcFolder
125: * String, the absolute path of the file to add
126: * @param zip
127: * ZipOutputStram, the stream to use to write the given file.
128: */
129: private static void addFolderToZip(final String path,
130: final String srcFolder, final ZipOutputStream zip) {
131: File folder = new File(srcFolder);
132: String[] fileListe = folder.list();
133: for (String string : fileListe) {
134: addToZip(path, srcFolder + "/" + string, zip);
135: }
136: }
137: }
|