[Arakhnę-Dev] [267] * Add functions to zip or unzip folders. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 267
Author: galland
Date: 2011-08-22 10:03:28 +0200 (Mon, 22 Aug 2011)
Log Message:
-----------
* Add functions to zip or unzip folders.
Modified Paths:
--------------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/file/URLConnectionTest.java
trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/resource/URLConnectionTest.java
trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test.txt
Added Paths:
-----------
trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test2.txt
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2011-08-21 21:22:00 UTC (rev 266)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java 2011-08-22 08:03:28 UTC (rev 267)
@@ -48,7 +48,6 @@
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
-
/** An utility class that permits to deal with filenames.
*
* @author $Author: galland$
@@ -1149,6 +1148,10 @@
* The content of the second file will be lost.
* This copy function allows to do a copy between two different
* partitions.
+ * <p>
+ * If the <var>out</var> parameter is a directory, the output file
+ * is a file with the same basename as the input and inside
+ * the <var>ou</var> directory.
*
* @param in is the file to copy.
* @param out is the target file
@@ -1159,27 +1162,16 @@
public static void copy(File in, File out) throws IOException {
assert(in!=null);
assert(out!=null);
- FileChannel inChannel = new FileInputStream(in).getChannel();
- FileChannel outChannel = new FileOutputStream(out).getChannel();
- try {
- // apparently has trouble copying large files on Windows
- if (OperatingSystem.WIN.isCurrentOS()) {
- // magic number for Windows, 64Mb - 32Kb
- int maxCount = (64 * 1024 * 1024) - (32 * 1024);
- long size = inChannel.size();
- long position = 0;
- while ( position < size ) {
- position += inChannel.transferTo( position, maxCount, outChannel );
- }
- }
- else {
- inChannel.transferTo(0, inChannel.size(), outChannel);
- }
+
+ File outFile = out;
+ if (out.isDirectory()) {
+ outFile = new File(out, largeBasename(in));
}
- finally {
- if (inChannel!=null) inChannel.close();
- if (outChannel!=null) outChannel.close();
- }
+
+ copy(
+ new FileInputStream(in),
+ (int)in.length(),
+ new FileOutputStream(outFile));
}
/** Copy the first file into the second file.
@@ -1214,10 +1206,37 @@
public static void copy(URL in, File out) throws IOException {
assert(in!=null);
assert(out!=null);
+
+ File outFile = out;
+ if (out.isDirectory()) {
+ outFile = new File(out, largeBasename(in));
+ }
+
URLConnection connection = in.openConnection();
- ReadableByteChannel inChannel = Channels.newChannel(connection.getInputStream());
- FileChannel outChannel = new FileOutputStream(out).getChannel();
- int size = connection.getContentLength();
+ copy(
+ connection.getInputStream(),
+ connection.getContentLength(),
+ new FileOutputStream(outFile));
+ }
+
+ /** Copy the first file into the second file.
+ * <p>
+ * The content of the second file will be lost.
+ * This copy function allows to do a copy between two different
+ * partitions.
+ *
+ * @param in is the input stream to read.
+ * @param inSize is the total size of the input stream.
+ * @param out is the output stream.
+ * @throws IOException
+ * @since 6.2
+ */
+ public static void copy(InputStream in, int inSize, FileOutputStream out) throws IOException {
+ assert(in!=null);
+ assert(out!=null);
+ ReadableByteChannel inChannel = Channels.newChannel(in);
+ FileChannel outChannel = out.getChannel();
+ int size = inSize;
try {
// apparently has trouble copying large files on Windows
if (size<0 || OperatingSystem.WIN.isCurrentOS()) {
@@ -1239,7 +1258,7 @@
if (outChannel!=null) outChannel.close();
}
}
-
+
/** Replies the user home directory.
*
* @return the home directory of the current user.
@@ -2299,6 +2318,21 @@
* @throws IOException when is is impossible to retreive canonical paths.
*/
public static File makeRelative(File filenameToMakeRelative, File rootPath) throws IOException {
+ return makeRelative(filenameToMakeRelative, rootPath, true);
+ }
+
+ /**
+ * Make the given filename relative to the given root path.
+ *
+ * @param filenameToMakeRelative is the name to make relative.
+ * @param rootPath is the root path from which the relative path will be set.
+ * @param appendCurrentDirectorySymbol indicates if "./" should be append at the
+ * begining of the relative filename.
+ * @return a relative filename.
+ * @throws IOException when is is impossible to retreive canonical paths.
+ */
+ private static File makeRelative(File filenameToMakeRelative, File rootPath, boolean appendCurrentDirectorySymbol) throws IOException {
+
if (filenameToMakeRelative==null || rootPath==null)
throw new IllegalArgumentException();
@@ -2313,7 +2347,9 @@
String relPath = makeRelative(parts1, parts2, filenameToMakeRelative.getName());
- return new File(CURRENT_DIRECTORY, relPath);
+ if (appendCurrentDirectorySymbol)
+ return new File(CURRENT_DIRECTORY, relPath);
+ return new File(relPath);
}
/**
@@ -2491,23 +2527,31 @@
byte[] buffer = new byte[2048];
int len;
- File file;
+ File file, relativeFile;
File rootDirectory = (input.isDirectory()) ? input : input.getParentFile();
while (!candidates.isEmpty()) {
file = candidates.removeFirst();
assert(file!=null);
+
+ if (file.getAbsoluteFile().equals(rootDirectory.getAbsoluteFile()))
+ relativeFile = null;
+ else
+ relativeFile = makeRelative(file, rootDirectory, false);
+
if (file.isDirectory()) {
- ZipEntry zipEntry = new ZipEntry(makeRelative(file, rootDirectory).getName()+"/"); //$NON-NLS-1$
- zos.putNextEntry(zipEntry);
- zos.closeEntry();
+ if (relativeFile!=null) {
+ ZipEntry zipEntry = new ZipEntry(relativeFile.getName()+"/"); //$NON-NLS-1$
+ zos.putNextEntry(zipEntry);
+ zos.closeEntry();
+ }
candidates.addAll(Arrays.asList(file.listFiles()));
}
- else {
+ else if (relativeFile!=null) {
FileInputStream fis = new FileInputStream(file);
try {
- ZipEntry zipEntry = new ZipEntry(makeRelative(file, rootDirectory).toString());
+ ZipEntry zipEntry = new ZipEntry(relativeFile.toString());
zos.putNextEntry(zipEntry);
while ((len=fis.read(buffer))>0) {
zos.write(buffer, 0, len);
@@ -2541,8 +2585,25 @@
if (!output.isDirectory()) throw new IOException("not a directory: "+output); //$NON-NLS-1$
ZipInputStream zis = null;
try {
+ byte[] buffer = new byte[2048];
+ int len;
+
zis = new ZipInputStream(input);
-
+ ZipEntry zipEntry = zis.getNextEntry();
+ while (zipEntry!=null) {
+ String name = zipEntry.getName();
+ File outFile = new File(output,name).getCanonicalFile();
+ FileOutputStream fos = new FileOutputStream(outFile);
+ try {
+ while ((len=zis.read(buffer))>0) {
+ fos.write(buffer, 0, len);
+ }
+ }
+ finally {
+ fos.close();
+ }
+ zipEntry = zis.getNextEntry();
+ }
}
finally {
if (zis!=null) zis.close();
Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2011-08-21 21:22:00 UTC (rev 266)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java 2011-08-22 08:03:28 UTC (rev 267)
@@ -18,9 +18,14 @@
package org.arakhne.vmutil;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
@@ -1404,5 +1409,68 @@
new URL("file:../a/c/d/e"), //$NON-NLS-1$
FileSystem.makeCanonicalURL(new URL("file:../a/b/../c/./d/e"))); //$NON-NLS-1$
}
+
+ private String readInputStream(InputStream is) throws IOException {
+ StringBuffer b = new StringBuffer();
+ byte[] buffer = new byte[2048];
+ int len;
+ while ((len=is.read(buffer))>0) {
+ b.append(new String(buffer, 0, len));
+ }
+ is.close();
+ return b.toString();
+ }
+
+ private void createZip(File testArchive) throws IOException {
+ File testDir = FileSystem.createTempDirectory("unittest", null); //$NON-NLS-1$
+ FileSystem.deleteOnExit(testDir);
+ FileSystem.copy(FileSystemTest.class.getResource("test.txt"), testDir); //$NON-NLS-1$
+ FileSystem.copy(FileSystemTest.class.getResource("test2.txt"), testDir); //$NON-NLS-1$
+ FileSystem.zipFile(testDir, testArchive);
+ }
+
+ /**
+ * @throws IOException
+ */
+ public void testZipFileFile() throws IOException {
+ File testArchive = File.createTempFile("unittest", ".zip"); //$NON-NLS-1$ //$NON-NLS-2$
+ testArchive.deleteOnExit();
+
+ createZip(testArchive);
+
+ ZipFile zipFile = new ZipFile(testArchive);
+ ZipEntry zipEntry = zipFile.getEntry("test.txt"); //$NON-NLS-1$
+ assertNotNull(zipEntry);
+ assertEquals("TEST1: FOR UNIT TEST ONLY", readInputStream(zipFile.getInputStream(zipEntry))); //$NON-NLS-1$
+
+ zipEntry = zipFile.getEntry("test2.txt"); //$NON-NLS-1$
+ assertNotNull(zipEntry);
+ assertEquals("TEST2: FOR UNIT TEST ONLY", readInputStream(zipFile.getInputStream(zipEntry))); //$NON-NLS-1$
+
+ zipFile.close();
+ }
+
+ /**
+ * @throws IOException
+ */
+ public void testUnzipFileFile() throws IOException {
+ File testArchive = File.createTempFile("unittest", ".zip"); //$NON-NLS-1$ //$NON-NLS-2$
+ testArchive.deleteOnExit();
+ createZip(testArchive);
+
+ File testDir = FileSystem.createTempDirectory("unittest", null); //$NON-NLS-1$
+ FileSystem.deleteOnExit(testDir);
+
+ FileSystem.unzipFile(testArchive, testDir);
+
+ assertTrue(testDir.isDirectory());
+
+ File file = new File(testDir, "test.txt"); //$NON-NLS-1$
+ assertEquals("TEST1: FOR UNIT TEST ONLY", readInputStream(new FileInputStream(file))); //$NON-NLS-1$
+
+ file = new File(testDir, "test2.txt"); //$NON-NLS-1$
+ assertEquals("TEST2: FOR UNIT TEST ONLY", readInputStream(new FileInputStream(file))); //$NON-NLS-1$
+ }
+
}
Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/file/URLConnectionTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/file/URLConnectionTest.java 2011-08-21 21:22:00 UTC (rev 266)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/file/URLConnectionTest.java 2011-08-22 08:03:28 UTC (rev 267)
@@ -81,7 +81,7 @@
*/
public void testGetHeaderFieldInt() {
assertEquals("text/plain", this.connection.getHeaderField(0)); //$NON-NLS-1$
- assertEquals("19", this.connection.getHeaderField(1)); //$NON-NLS-1$
+ assertEquals("25", this.connection.getHeaderField(1)); //$NON-NLS-1$
assertNotNull(this.connection.getHeaderField(2));
assertNull(this.connection.getHeaderField(3));
}
@@ -90,7 +90,7 @@
*/
public void testGetHeaderFieldString() {
assertEquals("text/plain", this.connection.getHeaderField("content-type")); //$NON-NLS-1$ //$NON-NLS-2$
- assertEquals("19", this.connection.getHeaderField("content-length")); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals("25", this.connection.getHeaderField("content-length")); //$NON-NLS-1$ //$NON-NLS-2$
assertNotNull(this.connection.getHeaderField("last-modified")); //$NON-NLS-1$
assertNull(this.connection.getHeaderField("expires")); //$NON-NLS-1$
}
@@ -102,7 +102,7 @@
assertNotNull(map);
assertEquals(3, map.size());
assertEquals(Collections.singletonList("text/plain"), map.get("content-type")); //$NON-NLS-1$ //$NON-NLS-2$
- assertEquals(Collections.singletonList("19"), map.get("content-length")); //$NON-NLS-1$ //$NON-NLS-2$
+ assertEquals(Collections.singletonList("25"), map.get("content-length")); //$NON-NLS-1$ //$NON-NLS-2$
assertNotNull(map.get("last-modified")); //$NON-NLS-1$
assertNull(map.get("expires")); //$NON-NLS-1$
}
@@ -115,7 +115,7 @@
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
br.close();
- assertEquals("FOR UNIT TEST ONLY ", line); //$NON-NLS-1$
+ assertEquals("TEST1: FOR UNIT TEST ONLY", line); //$NON-NLS-1$
}
/**
Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/resource/URLConnectionTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/resource/URLConnectionTest.java 2011-08-21 21:22:00 UTC (rev 266)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/resource/URLConnectionTest.java 2011-08-22 08:03:28 UTC (rev 267)
@@ -68,7 +68,7 @@
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
br.close();
- assertEquals("FOR UNIT TEST ONLY ", line); //$NON-NLS-1$
+ assertEquals("TEST1: FOR UNIT TEST ONLY", line); //$NON-NLS-1$
}
}
Modified: trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test.txt
===================================================================
--- trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test.txt 2011-08-21 21:22:00 UTC (rev 266)
+++ trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test.txt 2011-08-22 08:03:28 UTC (rev 267)
@@ -1 +1 @@
-FOR UNIT TEST ONLY
\ No newline at end of file
+TEST1: FOR UNIT TEST ONLY
\ No newline at end of file
Added: trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test2.txt
===================================================================
--- trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test2.txt (rev 0)
+++ trunk/arakhneVmutils/java/src/test/resources/org/arakhne/vmutil/test2.txt 2011-08-22 08:03:28 UTC (rev 267)
@@ -0,0 +1 @@
+TEST2: FOR UNIT TEST ONLY
\ No newline at end of file