1 /* -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 // Copyright (C) 2004 Novell, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining a
10 // copy of this software and associated documentation files (the "Software"),
11 // to deal in the Software without restriction, including without limitation
12 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 // and/or sell copies of the Software, and to permit persons to whom the
14 // Software is furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 // DEALINGS IN THE SOFTWARE.
30 using System
.Collections
;
33 using ICSharpCode
.SharpZipLib
;
34 using ICSharpCode
.SharpZipLib
.Zip
;
35 using ICSharpCode
.SharpZipLib
.GZip
;
36 using ICSharpCode
.SharpZipLib
.BZip2
;
37 using ICSharpCode
.SharpZipLib
.Tar
;
39 namespace Beagle
.Util
{
41 public class Archive
: Stream
46 delegate string GetNextEntryType ();
47 GetNextEntryType getNextEntry
;
50 public Archive (string filename
, string mimeType
) {
51 this.uri
= StringFu
.PathToQuotedFileUri (filename
);
52 baseStream
= new FileStream (filename
,
55 case "application/zip":
56 baseStream
= new ZipInputStream (baseStream
);
57 getNextEntry
= new GetNextEntryType (GetNextEntryZip
);
60 case "application/x-bzip-compressed-tar":
61 baseStream
= new BZip2InputStream (baseStream
);
62 baseStream
= new TarInputStream (baseStream
);
63 getNextEntry
= new GetNextEntryType (GetNextEntryTar
);
64 method
= "#bzip2:#tar:";
66 case "application/x-compressed-tar":
67 baseStream
= new GZipInputStream (baseStream
);
68 baseStream
= new TarInputStream (baseStream
);
69 getNextEntry
= new GetNextEntryType (GetNextEntryTar
);
70 method
= "#gzip:#tar:";
72 case "application/x-tar":
73 baseStream
= new TarInputStream (baseStream
);
74 getNextEntry
= new GetNextEntryType (GetNextEntryTar
);
77 case "application/x-gzip":
78 baseStream
= new GZipInputStream (baseStream
);
79 getNextEntry
= new GetNextEntryType (GetNextEntrySingle
);
82 case "application/x-bzip":
83 baseStream
= new BZip2InputStream (baseStream
);
84 getNextEntry
= new GetNextEntryType (GetNextEntrySingle
);
88 throw new ArgumentException ("Invalid or unsupported mime type.");
92 /* Returns the URI of the next string. */
93 public string GetNextEntry () {
94 return getNextEntry();
97 public string GetNextEntryZip () {
98 ZipInputStream inputStream
= baseStream
as ZipInputStream
;
99 ZipEntry entry
= inputStream
.GetNextEntry();
101 return uri
+ method
+ entry
.Name
;
106 public string GetNextEntrySingle () {
114 public string GetNextEntryTar () {
115 TarInputStream inputStream
= baseStream
as TarInputStream
;
116 TarEntry entry
= inputStream
.GetNextEntry();
118 return uri
+ method
+ entry
.Name
;
123 public override int Read (byte[] buffer
, int offset
, int length
) {
124 return baseStream
.Read (buffer
, offset
, length
);
127 public override IAsyncResult
BeginRead (byte[] buffer
, int offset
, int length
,
128 AsyncCallback cback
, object state
)
130 return baseStream
.BeginRead (buffer
, offset
, length
, cback
, state
);
133 public override int EndRead(IAsyncResult async_result
) {
134 return baseStream
.EndRead (async_result
);
137 public override void Write (byte[] buffer
, int offset
, int length
) {
138 throw new NotSupportedException ();
140 public override void Flush () {
141 throw new NotSupportedException ();
143 public override long Seek (long offset
, SeekOrigin origin
) {
144 throw new NotSupportedException ();
146 public override void SetLength (long value) {
147 throw new System
.NotSupportedException();
149 public override bool CanRead
{
151 return baseStream
.CanRead
;
154 public override bool CanSeek
{
159 public override bool CanWrite
{
164 public override long Length
{
166 throw new System
.NotSupportedException();
169 throw new System
.NotSupportedException();
172 public override long Position
{
174 throw new System
.NotSupportedException();
177 throw new System
.NotSupportedException();