[Schmitzm-commits] r1475 - trunk/schmitzm-core/src/main/java/de/schmitzm/io
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Sun Jan 30 02:00:37 CET 2011
Author: alfonx
Date: 2011-01-30 02:00:37 +0100 (Sun, 30 Jan 2011)
New Revision: 1475
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
Log:
added some more zip util methods
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java 2011-01-30 01:00:22 UTC (rev 1474)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java 2011-01-30 01:00:37 UTC (rev 1475)
@@ -1295,41 +1295,60 @@
throws IOException {
// create a new File object based on the directory we
File zipDir = new File(zipMePath);
+
// get a listing of the directory content
- String[] dirList = zipDir.list();
- byte[] readBuffer = new byte[2156];
- int bytesIn = 0;
+ String[] dirList;
+
+ // if (zipDir.isDirectory())
+ dirList = zipDir.list();
+ // else {
+ // dirList = new String[] { zipDir.getName() };
+ // zipDir = zipDir.getParentFile();
+ // cutOff -= zipDir.getName().length();
+ // }
+
// loop through dirList, and zip the files
for (int i = 0; i < dirList.length; i++) {
File f = new File(zipDir, dirList[i]);
- if (f.isDirectory()) {
- // if the File object is a directory, call this
- // function again to add its content recursively
- String filePath = f.getPath();
- zipDir(filePath, zos, cutOff);
- // loop again
- continue;
- }
- // if we reached here, the File object f was not
- // create a FileInputStream on top of f
- FileInputStream fis = new FileInputStream(f);
- String pathInZip = f.getPath();
- pathInZip = pathInZip.substring(cutOff);
- ZipEntry anEntry = new ZipEntry(pathInZip);
- // place the zip entry in the ZipOutputStream object
- zos.putNextEntry(anEntry);
- // now write the content of the file to the ZipOutputStream
- while ((bytesIn = fis.read(readBuffer)) != -1) {
- zos.write(readBuffer, 0, bytesIn);
- }
- // close the Stream
- fis.close();
+ zipFile(f, zos, cutOff);
}
}
+ public static void zipFile(File file, ZipOutputStream zos, int cutOff)
+ throws IOException {
+ byte[] readBuffer = new byte[2156];
+ int bytesIn = 0;
+
+ if (file.isDirectory()) {
+ // if the File object is a directory, call this
+ // function again to add its content recursively
+ String filePath = file.getPath();
+ zipDir(filePath, zos, cutOff);
+ // loop again
+ return;
+ }
+ // if we reached here, the File object f was not
+ // create a FileInputStream on top of f
+ FileInputStream fis = new FileInputStream(file);
+ String pathInZip = file.getPath();
+ pathInZip = pathInZip.substring(cutOff);
+ if (pathInZip.equals("")) {
+ System.out.println("new entry=" + pathInZip);
+ }
+ ZipEntry anEntry = new ZipEntry(pathInZip);
+ // place the zip entry in the ZipOutputStream object
+ zos.putNextEntry(anEntry);
+ // now write the content of the file to the ZipOutputStream
+ while ((bytesIn = fis.read(readBuffer)) != -1) {
+ zos.write(readBuffer, 0, bytesIn);
+ }
+ // close the Stream
+ fis.close();
+ }
+
public static void zipDir(File zipMe, ZipOutputStream zos, File insertRelTo)
throws IOException {
- zipDir(zipMe.getAbsolutePath(), zos, zipMe.getAbsolutePath().length());
+ zipFile(zipMe, zos, zipMe.getAbsolutePath().length());
}
@@ -1376,6 +1395,7 @@
static public void unzipEntry(ZipFile zipfile, ZipEntry entry,
File outputDir) throws IOException {
+ System.out.println("next=" + entry.getName());
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
@@ -1463,4 +1483,9 @@
}
}
+ public static void zipFile(File delFile, ZipOutputStream zos, File cutOff)
+ throws IOException {
+ zipFile(delFile, zos, cutOff.getAbsolutePath().length());
+ }
+
}
More information about the Schmitzm-commits
mailing list