[ease/inspector] Move archive extraction code into Temp to allow for reuse.



commit 79d2077671fc11966217203fd4b1d305c0280ffc
Author: Nate Stedman <natesm gmail com>
Date:   Fri Jun 4 20:21:38 2010 -0400

    Move archive extraction code into Temp to allow for reuse.

 src/JSONParser.vala |   37 ++-----------------------------------
 src/Temp.vala       |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 35 deletions(-)
---
diff --git a/src/JSONParser.vala b/src/JSONParser.vala
index c21ba7b..e6f4542 100644
--- a/src/JSONParser.vala
+++ b/src/JSONParser.vala
@@ -19,9 +19,7 @@
  * Parses JSON files to load Ease { link Document}s
  */
 public static class Ease.JSONParser
-{
-	private const int ARCHIVE_BUFFER = 4096;
-	
+{	
 	/**
 	 * Parses a document JSON file, creating a { link Document}.
 	 *
@@ -63,39 +61,8 @@ public static class Ease.JSONParser
 	 */
 	public static Theme theme(string filename) throws GLib.Error
 	{
-		// initialize the .easetheme archive
-		var archive = new Archive.Read();
-		
-		// automatically detect archive type
-		archive.support_compression_all();
-		archive.support_format_all();
-		
-		// open the archive
-		archive.open_filename(filename, ARCHIVE_BUFFER);
-		
-		// extract the archive
-		string path = Temp.request();
-		
-		weak Archive.Entry entry;
-		while (archive.next_header(out entry) == Archive.Result.OK)
-		{
-			var fpath = Path.build_path("/", path, entry.pathname());
-			var file = GLib.File.new_for_path(fpath);
-			if (Posix.S_ISDIR(entry.mode()))
-			{
-				file.make_directory_with_parents(null);
-			}
-			else
-			{
-				file.create(FileCreateFlags.REPLACE_DESTINATION, null);
-				int fd = Posix.open(fpath, Posix.O_WRONLY, 0644);
-				archive.read_data_into_fd(fd);
-				Posix.close(fd);
-			}
-		}
-		
 		var theme = new Theme();
-		theme.path = path;
+		theme.path = Temp.extract(filename);
 	
 		var parser = new Json.Parser();
 		
diff --git a/src/Temp.vala b/src/Temp.vala
index 699dd14..ca5ed88 100644
--- a/src/Temp.vala
+++ b/src/Temp.vala
@@ -22,6 +22,8 @@ public static class Ease.Temp : Object
 {
 	private static int index = 0;
 	
+	private const int ARCHIVE_BUFFER = 4096;
+	
 	/**
 	 * Requests a temporary directory.
 	 *
@@ -67,4 +69,50 @@ public static class Ease.Temp : Object
 		
 		return tmp;
 	}
+	
+	/**
+	 * Creates a temporary directory and extracts an archive to it.
+	 *
+	 * extract() uses libarchive for extraction. It will automatically request
+	 * a new temporary directory, extract the archive, and return the path
+	 * to the extracted files.
+	 *
+	 * @param filename The path of the archive to extract.
+	 */
+	public static string extract(string filename) throws GLib.Error
+	{
+		// initialize the archive
+		var archive = new Archive.Read();
+		
+		// automatically detect archive type
+		archive.support_compression_all();
+		archive.support_format_all();
+		
+		// open the archive
+		archive.open_filename(filename, ARCHIVE_BUFFER);
+		
+		// create a temporary directory to extract to
+		string path = request();
+		
+		// extract the archive
+		weak Archive.Entry entry;
+		while (archive.next_header(out entry) == Archive.Result.OK)
+		{
+			var fpath = Path.build_path("/", path, entry.pathname());
+			var file = GLib.File.new_for_path(fpath);
+			if (Posix.S_ISDIR(entry.mode()))
+			{
+				file.make_directory_with_parents(null);
+			}
+			else
+			{
+				file.create(FileCreateFlags.REPLACE_DESTINATION, null);
+				int fd = Posix.open(fpath, Posix.O_WRONLY, 0644);
+				archive.read_data_into_fd(fd);
+				Posix.close(fd);
+			}
+		}
+		
+		return path;
+	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]