[Arakhnę-Dev] [264] * Add functions to zip or unzip folders.

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


Revision: 264
Author:   galland
Date:     2011-08-21 16:45:08 +0200 (Sun, 21 Aug 2011)
Log Message:
-----------
* Add functions to zip or unzip folders.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/pom.xml
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java

Modified: trunk/arakhneVmutils/java/pom.xml
===================================================================
--- trunk/arakhneVmutils/java/pom.xml	2011-08-19 14:44:31 UTC (rev 263)
+++ trunk/arakhneVmutils/java/pom.xml	2011-08-21 14:45:08 UTC (rev 264)
@@ -35,7 +35,7 @@
 			Overrule the default pom source directory to match our generated
 			sources so the compiler will pick them up
 		-->
-		<sourceDirectory>${project.build.directory}/generated-sources/java</sourceDirectory>
+		<!-- <sourceDirectory>${project.build.directory}/generated-sources/java</sourceDirectory> -->
 		<plugins>
 			<plugin>
 				<artifactId>maven-jar-plugin</artifactId>

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-19 14:44:31 UTC (rev 263)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2011-08-21 14:45:08 UTC (rev 264)
@@ -24,6 +24,8 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URI;
@@ -35,11 +37,15 @@
 import java.nio.channels.FileChannel;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.charset.Charset;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
 
 
 /** An utility class that permits to deal with filenames.
@@ -2460,6 +2466,110 @@
 		return url;
 	}
 
+	/**
+	 * Create a zip file from the given input file.
+	 * If the input file is a directory, the content of the directory is zipped.
+	 * If the input file is a standard file, it is zipped.
+	 * 
+	 * @param input
+	 * @param output
+	 * @throws IOException
+	 * @since 6.2
+	 */
+	public static void zipFile(File input, OutputStream output) throws IOException {
+		ZipOutputStream zos = null;
+		try {
+			zos = new ZipOutputStream(output);
+			
+			if (input==null) return;
+
+			LinkedList<File> candidates = new LinkedList<File>();
+			candidates.add(input);
+			
+			byte[] buffer = new byte[2048];
+			int len;
+			File file;
+			
+			File rootDirectory = (input.isDirectory()) ? input : input.getParentFile();
+			
+			while (!candidates.isEmpty()) {
+				file = candidates.removeFirst();
+				assert(file!=null);
+				if (file.isDirectory()) {
+					ZipEntry zipEntry = new ZipEntry(makeRelative(file, rootDirectory).getName()+"/"); //$NON-NLS-1$
+					zos.putNextEntry(zipEntry);
+					zos.closeEntry();
+					candidates.addAll(Arrays.asList(file.listFiles()));
+				}
+				else {
+					FileInputStream fis = new FileInputStream(file);
+					try {
+						ZipEntry zipEntry = new ZipEntry(makeRelative(file, rootDirectory).toString());
+						zos.putNextEntry(zipEntry);
+						while ((len=fis.read(buffer))>0) {
+							zos.write(buffer, 0, len);
+						}
+						zos.closeEntry();
+					}
+					finally {
+						fis.close();
+					}
+				}
+			}
+		}
+		finally {
+			if (zos!=null) zos.close();
+		}
+	}
+	
+	/**
+	 * Unzip the given stream and write out the file in the output.
+	 * If the input file is a directory, the content of the directory is zipped.
+	 * If the input file is a standard file, it is zipped.
+	 * 
+	 * @param input
+	 * @param output
+	 * @throws IOException
+	 * @since 6.2
+	 */
+	public static void unzipFile(InputStream input, File output) throws IOException {
+		if (output==null) return;
+		output.mkdirs();
+		if (!output.isDirectory()) throw new IOException("not a directory: "+output); //$NON-NLS-1$
+		ZipInputStream zis = null;
+		try {
+			zis = new ZipInputStream(input);
+			
+		}
+		finally {
+			if (zis!=null) zis.close();
+		}
+	}
+
+	/**
+	 * Create a zip file from the given input file.
+	 * 
+	 * @param input
+	 * @param output
+	 * @throws IOException
+	 * @since 6.2
+	 */
+	public static void zipFile(File input, File output) throws IOException {
+		zipFile(input, new FileOutputStream(output));
+	}
+
+	/**
+	 * Unzip a file into the output directory.
+	 * 
+	 * @param input
+	 * @param output
+	 * @throws IOException
+	 * @since 6.2
+	 */
+	public static void unzipFile(File input, File output) throws IOException {
+		unzipFile(new FileInputStream(input), output);
+	}
+
 	/** Hook to recursively delete files on JVM exit. 
 	 * 
 	 * @author $Author: galland$


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