[Schmitzm-commits] r1138 - in trunk: src/schmitzm/io src_junit/schmitzm src_junit/schmitzm/io src_junit/schmitzm/swing src_junit/skrueger/i8n
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Fri Oct 15 23:31:20 CEST 2010
Author: alfonx
Date: 2010-10-15 23:31:18 +0200 (Fri, 15 Oct 2010)
New Revision: 1138
Added:
trunk/src_junit/schmitzm/io/
trunk/src_junit/schmitzm/io/IOUtilTest.java
Modified:
trunk/src/schmitzm/io/IOUtil.java
trunk/src_junit/schmitzm/swing/SwingUtilTest.java
trunk/src_junit/schmitzm/swing/TestingUtil.java
trunk/src_junit/skrueger/i8n/SwitchLanguageDialogTest.java
Log:
New method in IOUtil to create zip files from folders with relative paths inside.
Modified: trunk/src/schmitzm/io/IOUtil.java
===================================================================
--- trunk/src/schmitzm/io/IOUtil.java 2010-10-15 15:21:36 UTC (rev 1137)
+++ trunk/src/schmitzm/io/IOUtil.java 2010-10-15 21:31:18 UTC (rev 1138)
@@ -43,6 +43,8 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.channels.FileChannel;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
@@ -666,13 +668,14 @@
if (tempPath == null || tempPath.isEmpty()
|| !new File(tempPath).exists()) {
-
+
if (SystemUtils.IS_OS_WINDOWS) {
tempPath = "C:\tmp";
} else {
tempPath = "/tmp";
}
- LOGGER.warn("Temporary directory can't be found. Using '"+tempPath+"'");
+ LOGGER.warn("Temporary directory can't be found. Using '"
+ + tempPath + "'");
}
while (tempPath.endsWith(File.separator))
@@ -784,43 +787,47 @@
}
}
-
/**
* Bereinigt Pfadangaben, z.b. "C:\Programme" zu "C:\\Programme" und
- * "Eigene%20Dateien" zu "Eigene Dateien". Liefert einen leeren String wenn <code>null</code> übergeben wurde.
+ * "Eigene%20Dateien" zu "Eigene Dateien". Liefert einen leeren String wenn
+ * <code>null</code> übergeben wurde.
*/
public static String escapePath(String path) {
- if (path == null) return "";
+ if (path == null)
+ return "";
path = path.replace("\\", "\\\\");
path = path.replace("%20", " ");
return path;
}
-
/**
* Bereinigt Pfadangaben, z.b. "C:\Programme" zu "C:\\Programme" und
- * "Eigene%20Dateien" zu "Eigene Dateien". Liefert einen leeren String wenn <code>null</code> übergeben wurde.
+ * "Eigene%20Dateien" zu "Eigene Dateien". Liefert einen leeren String wenn
+ * <code>null</code> übergeben wurde.
*/
public static String escapePath(URL url) {
- if (url == null) return "";
+ if (url == null)
+ return "";
if (url.getProtocol().equals("file")) {
- // If this URL points to an file://, return it as a file path rather than an URL.
- return escapePath( DataUtilities.urlToFile(url) );
+ // If this URL points to an file://, return it as a file path rather
+ // than an URL.
+ return escapePath(DataUtilities.urlToFile(url));
}
return escapePath(url.toExternalForm());
}
/**
- * Liefert eine bereinigt Absolute-Pfadangaben, z.b. "C:\Programme" zu "C:\\Programme" und
- * "Eigene%20Dateien" zu "Eigene Dateien". Liefert einen leeren String wenn <code>null</code> übergeben wurde.
+ * Liefert eine bereinigt Absolute-Pfadangaben, z.b. "C:\Programme" zu
+ * "C:\\Programme" und "Eigene%20Dateien" zu "Eigene Dateien". Liefert einen
+ * leeren String wenn <code>null</code> übergeben wurde.
*/
public static String escapePath(File file) {
- if (file == null) return "";
+ if (file == null)
+ return "";
return escapePath(file.getAbsolutePath());
}
-
/**
* Converts any filename to a String that is more Linux/Windows/Charset
@@ -893,15 +900,14 @@
* letter or an underscore.
*/
if (filename.length() > 0)
- filename = filename.replaceAll("^(\\d)", "n"
- + filename.substring(0, 1));
+ filename = filename.replaceAll("^(\\d)",
+ "n" + filename.substring(0, 1));
if (!orig.equals(filename))
LOGGER.debug("Cleaned a filename from " + orig + " to " + filename);
return filename;
}
-
/**
* Copy file/folder to file/folder but doesn't throw an Exception
*
@@ -967,8 +973,8 @@
URISyntaxException {
final String sourceName = IOUtil.urlToFile(source).getName();
- final String cleanName = cleanFilenames ? IOUtil.cleanFilename(sourceName)
- : sourceName;
+ final String cleanName = cleanFilenames ? IOUtil
+ .cleanFilename(sourceName) : sourceName;
if (destination.isDirectory()) {
destination = new File(destination, cleanName);
@@ -979,7 +985,6 @@
FileUtils.copyURLToFile(source, destination);
}
-
/**
* Copy file or folder recursively to file or folder. All filenames are
* turned to lower case!
@@ -1079,4 +1084,66 @@
}
+ /**
+ * @param zipMePath
+ * @param zos
+ * @param cutOff
+ * use 0 for absolute paths.
+ * @throws IOException
+ */
+ public static void zipDir(String zipMePath, ZipOutputStream zos, int cutOff)
+ 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;
+ // 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();
+ }
+ }
+
+ public static void zipDir(File zipMe, ZipOutputStream zos, File insertRelTo)
+ throws IOException {
+ zipDir(zipMe.getAbsolutePath(), zos, zipMe.getAbsolutePath().length());
+
+ }
+
+ public static void zipDir(File zipMe, File zipFile, File insertRelTo)
+ throws IOException {
+ ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
+
+ try {
+ zipDir(zipMe, zos, insertRelTo);
+ } finally {
+ zos.finish();
+
+ }
+
+ }
+
}
Added: trunk/src_junit/schmitzm/io/IOUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/io/IOUtilTest.java 2010-10-15 15:21:36 UTC (rev 1137)
+++ trunk/src_junit/schmitzm/io/IOUtilTest.java 2010-10-15 21:31:18 UTC (rev 1138)
@@ -0,0 +1,38 @@
+package schmitzm.io;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+import org.junit.Test;
+
+public class IOUtilTest {
+ @Test
+ public void testZipDir() throws IOException {
+ // Verzeichnisstruktr erstellen
+ File d0 = new File(IOUtil.getTempDir(), "testDir"
+ + System.currentTimeMillis());
+ File d1 = new File(d0, "d1");
+ d1.mkdirs();
+
+ File f1 = new File(d1, "f1");
+ f1.createNewFile();
+ PrintWriter printWriter = new PrintWriter(f1);
+ printWriter.write("test");
+ printWriter.close();
+ assertTrue(f1.length() > 0l);
+
+ File zipFile = File.createTempFile("test", ".zip");
+ IOUtil.zipDir(d0, zipFile, d0);
+
+ System.out.println(d0);
+ System.out.println(f1);
+ System.out.println(zipFile);
+
+ assertTrue(zipFile.length() > 0l);
+
+ }
+
+}
Property changes on: trunk/src_junit/schmitzm/io/IOUtilTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Id URL
Name: svn:eol-style
+ native
Modified: trunk/src_junit/schmitzm/swing/SwingUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/swing/SwingUtilTest.java 2010-10-15 15:21:36 UTC (rev 1137)
+++ trunk/src_junit/schmitzm/swing/SwingUtilTest.java 2010-10-15 21:31:18 UTC (rev 1138)
@@ -15,7 +15,7 @@
@Test
public void testPasswortInputOption() {
- if ( !TestingUtil.INTERACTIVE )
+ if ( !TestingUtil.isInteractive() )
return;
PasswordViewable pwInputOption = new ManualInputOption.PasswordViewable("Ein Passwort","Geheim");
Modified: trunk/src_junit/schmitzm/swing/TestingUtil.java
===================================================================
--- trunk/src_junit/schmitzm/swing/TestingUtil.java 2010-10-15 15:21:36 UTC (rev 1137)
+++ trunk/src_junit/schmitzm/swing/TestingUtil.java 2010-10-15 21:31:18 UTC (rev 1138)
@@ -357,7 +357,7 @@
public static void testGui(JFreeChart chart, String frameTitle, int waitMax)
throws Throwable {
- if (INTERACTIVE) {
+ if (isInteractive()) {
ChartFrame chartFrame = new ChartFrame(frameTitle, chart);
chartFrame.pack();
@@ -390,4 +390,8 @@
return perc;
}
+ public static boolean isInteractive() {
+ return INTERACTIVE;
+ }
+
}
Modified: trunk/src_junit/skrueger/i8n/SwitchLanguageDialogTest.java
===================================================================
--- trunk/src_junit/skrueger/i8n/SwitchLanguageDialogTest.java 2010-10-15 15:21:36 UTC (rev 1137)
+++ trunk/src_junit/skrueger/i8n/SwitchLanguageDialogTest.java 2010-10-15 21:31:18 UTC (rev 1138)
@@ -42,7 +42,7 @@
@Test
public void testDialog() throws Throwable {
- if (!TestingUtil.INTERACTIVE)
+ if (!TestingUtil.isInteractive())
// Test needed here because following GUI creation would crash
// without X11 DISPLAY
return;
@@ -55,7 +55,7 @@
@Test
public void testDialogDoesntAppearIf0OrOneLangOnly() throws Throwable {
- if (!TestingUtil.INTERACTIVE)
+ if (!TestingUtil.isInteractive())
// Test needed here because following GUI creation would crash
// without X11 DISPLAY
return;
More information about the Schmitzm-commits
mailing list