[Arakhnę-Dev] [339] * Force the filenames in the zipping and unzipping functions to be formated according to the URL /Unix standard.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 339
Author:   galland
Date:     2012-06-26 16:46:31 +0200 (Tue, 26 Jun 2012)
Log Message:
-----------
* Force the filenames in the zipping and unzipping functions to be formated according to the URL/Unix standard.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
    trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2012-05-15 18:59:34 UTC (rev 338)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2012-06-26 14:46:31 UTC (rev 339)
@@ -2661,6 +2661,10 @@
 		}
 		return url;
 	}
+	
+	private static String fileToURL(File file) {
+		return file.getPath().replace(File.separatorChar, '/');
+	}
 
 	/**
 	 * Create a zip file from the given input file.
@@ -2685,6 +2689,7 @@
 			byte[] buffer = new byte[2048];
 			int len;
 			File file, relativeFile;
+			String zipFilename;
 
 			File rootDirectory = (input.isDirectory()) ? input : input.getParentFile();
 
@@ -2699,7 +2704,8 @@
 				
 				if (file.isDirectory()) {
 					if (relativeFile!=null) {
-						ZipEntry zipEntry = new ZipEntry(relativeFile.getName()+"/"); //$NON-NLS-1$
+						zipFilename = fileToURL(relativeFile)+"/"; //$NON-NLS-1$
+						ZipEntry zipEntry = new ZipEntry(zipFilename);
 						zos.putNextEntry(zipEntry);
 						zos.closeEntry();
 					}
@@ -2708,7 +2714,8 @@
 				else if (relativeFile!=null) {
 					FileInputStream fis = new FileInputStream(file);
 					try {
-						ZipEntry zipEntry = new ZipEntry(relativeFile.toString());
+						zipFilename = fileToURL(relativeFile);
+						ZipEntry zipEntry = new ZipEntry(zipFilename);
 						zos.putNextEntry(zipEntry);
 						while ((len=fis.read(buffer))>0) {
 							zos.write(buffer, 0, len);

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2012-05-15 18:59:34 UTC (rev 338)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2012-06-26 14:46:31 UTC (rev 339)
@@ -1583,6 +1583,9 @@
 		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$
+		File subdir = new File(testDir, "subdir"); //$NON-NLS-1$
+		subdir.mkdirs();
+		FileSystem.copy(FileSystemTest.class.getResource("test.txt"), subdir); //$NON-NLS-1$
 		FileSystem.zipFile(testDir, testArchive);
 	}
 	
@@ -1605,6 +1608,10 @@
 		assertNotNull(zipEntry);
 		assertEquals("TEST2: FOR UNIT TEST ONLY", readInputStream(zipFile.getInputStream(zipEntry))); //$NON-NLS-1$
 		
+		zipEntry = zipFile.getEntry("subdir/test.txt"); //$NON-NLS-1$
+		assertNotNull(zipEntry);
+		assertEquals("TEST1: FOR UNIT TEST ONLY", readInputStream(zipFile.getInputStream(zipEntry))); //$NON-NLS-1$
+
 		zipFile.close();
 	}
 
@@ -1618,16 +1625,21 @@
 
 		File testDir = FileSystem.createTempDirectory("unittest", null); //$NON-NLS-1$
 		FileSystem.deleteOnExit(testDir);
-
+		File subDir = new File(testDir, "subdir"); //$NON-NLS-1$
+		
 		FileSystem.unzipFile(testArchive, testDir);
 		
 		assertTrue(testDir.isDirectory());
+		assertTrue(subDir.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$
+
+		file = new File(subDir, "test.txt"); //$NON-NLS-1$
+		assertEquals("TEST1: FOR UNIT TEST ONLY", readInputStream(new FileInputStream(file))); //$NON-NLS-1$
 	}
 
 }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/