2006-04-25 Hendrik Brandt <heb@gnome-de.org>
[beagle.git] / bludgeon / ZipFileObject.cs
blob31d7ea4d8e28028aabf22b1b67dc6d1a7ec1e31c
1 using System;
2 using System.Collections;
3 using System.IO;
4 using System.Text;
6 using ICSharpCode.SharpZipLib;
7 using ICSharpCode.SharpZipLib.Zip;
9 using Beagle.Util;
10 using Beagle;
12 namespace Bludgeon {
14 public class ZipFileObject : FileObject {
16 public ZipFileObject (DirectoryObject zip_root)
18 AllowChildren ();
20 ArrayList to_be_zipped;
21 to_be_zipped = new ArrayList (zip_root.Children);
23 foreach (FileSystemObject fso in to_be_zipped) {
24 zip_root.RemoveChild (fso, null);
25 this.AddChild (fso, null);
29 override protected string GetChildUri (FileSystemObject children)
31 // FIXME: What is the uri scheme for zip files?
32 string uri = this.Uri + "#" + children.Name;
33 return uri;
36 override public string MimeType {
37 get { return "application/x-zip"; }
40 override public string Extension {
41 get { return ".zip"; }
44 private void WriteObjectToZip (ZipOutputStream zip_out,
45 FileSystemObject fso,
46 EventTracker tracker)
49 MemoryStream memory = null;
51 string name;
52 name = fso.FullName.Substring (this.FullName.Length + 1);
53 if (fso is DirectoryObject)
54 name += "/";
56 ZipEntry entry;
57 entry = new ZipEntry (name);
58 entry.DateTime = fso.Timestamp;
60 if (fso is DirectoryObject)
61 entry.Size = 0;
62 else {
63 memory = new MemoryStream ();
64 ((FileObject) fso).AddToStream (memory, tracker);
65 entry.Size = memory.Length;
68 zip_out.PutNextEntry (entry);
69 if (memory != null) {
70 zip_out.Write (memory.ToArray (), 0, (int) memory.Length);
71 memory.Close ();
74 // If this is a directory, write out the children
75 if (fso is DirectoryObject)
76 foreach (FileSystemObject child in fso.Children)
77 WriteObjectToZip (zip_out, child, tracker);
80 override public void AddToStream (Stream stream, EventTracker tracker)
82 if (tracker != null)
83 tracker.ExpectingAdded (this.Uri);
85 UnclosableStream unclosable;
86 unclosable = new UnclosableStream (stream);
88 ZipOutputStream zip_out;
89 zip_out = new ZipOutputStream (unclosable);
91 foreach (FileSystemObject fso in Children)
92 WriteObjectToZip (zip_out, fso, tracker);
93 zip_out.Finish ();
94 zip_out.Close ();
97 override protected bool MatchesQueryPart (QueryPart part)
99 // The only thing that could match is the name, and we check
100 // for that already in FileSystemObject.MatchesQuery.
101 return false;