2009-11-08 Chris Toshok <toshok@ximian.com>
[moon.git] / tools / munxap / Zip.cs
bloba849f0c44b3119b8eab4c67f02802ae769956e33
1 /*
2 * Zip.cs.
4 * Contact:
5 * Moonlight List (moonlight-list@lists.ximian.com)
7 * Copyright 2009 Novell, Inc. (http://www.novell.com)
9 * See the LICENSE file included with the distribution for details.
13 using System;
14 using System.Collections;
15 using System.Collections.Generic;
16 using System.Diagnostics;
17 using System.IO;
19 class ZipContent {
20 string unzipped_filename;
22 public Zip Zip { get; set; }
23 public string Type { get { return Path.GetExtension (Filename); } }
24 public string Filename { get; set; }
25 public string UnzippedFilename {
26 get {
27 if (unzipped_filename == null) {
28 Zip.Unzip ();
29 unzipped_filename = Path.Combine (Zip.UnzipDirectory, Filename);
31 return unzipped_filename;
36 class Zip : IDisposable
38 List<ZipContent> files;
39 string unzip_directory;
40 bool unzipped;
42 public string FileName { get; set; }
43 public string UnzipDirectory {
44 get {
45 if (unzip_directory == null) {
46 unzip_directory = Path.Combine (Path.GetTempPath (), "munxap");
47 unzip_directory = Path.Combine (unzip_directory, Path.GetFileName (FileName));
48 unzip_directory = Path.Combine (unzip_directory, "pid-" + System.Diagnostics.Process.GetCurrentProcess ().Id.ToString ());
49 Directory.CreateDirectory (unzip_directory);
51 return unzip_directory;
54 public Zip (string filename)
56 FileName = filename;
59 ~Zip ()
61 Dispose ();
64 public void Dispose ()
66 if (unzip_directory != null) {
67 try {
68 Directory.Delete (unzip_directory);
69 } catch {
70 // Ignore any exceptions
75 public void Unzip ()
77 if (unzipped)
78 return;
80 using (System.Diagnostics.Process p = new System.Diagnostics.Process ()) {
81 p.StartInfo.FileName = "unzip";
82 p.StartInfo.Arguments = string.Format ("\"{0}\" -d \"{1}\" ", FileName, UnzipDirectory);
83 p.StartInfo.RedirectStandardOutput = true;
84 p.StartInfo.UseShellExecute = false;
85 p.Start ();
86 string output = p.StandardOutput.ReadToEnd ();
87 p.WaitForExit ();
88 if (p.ExitCode != 0)
89 throw new Exception (output);
92 unzipped = true;
95 public List<ZipContent> Files {
96 get {
97 if (files == null) {
98 files = new List<ZipContent> ();
100 using (System.Diagnostics.Process p = new System.Diagnostics.Process ()) {
101 p.StartInfo.FileName = "unzip";
102 p.StartInfo.Arguments = "-lqq \"" + FileName + "\"";
103 p.StartInfo.RedirectStandardOutput = true;
104 p.StartInfo.UseShellExecute = false;
105 p.Start ();
106 string output = p.StandardOutput.ReadToEnd ();
107 foreach (string line in output.Split (new char [] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries)) {
108 string [] args = line.Split (new char [] { ' '}, StringSplitOptions.RemoveEmptyEntries);
109 if (args.Length < 4)
110 continue;
112 ZipContent content = new ZipContent ();
113 content.Zip = this;
114 content.Filename = args [3];
115 files.Add (content);
119 return files;