Fixed #374055:Only the first "tag" is detected in digikam.
[beagle.git] / Util / Archive.cs
blob26a35f8174f5ebd00f08d9caf8d407c8c1fa471f
1 /* -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 //
3 // Archive.cs
4 //
5 // Copyright (C) 2004 Novell, Inc.
6 //
8 //
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.
29 using System;
30 using System.Collections;
31 using System.IO;
32 using System.Text;
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
43 Stream baseStream;
44 string uri;
45 string method;
46 delegate string GetNextEntryType ();
47 GetNextEntryType getNextEntry;
48 bool first = true;
50 public Archive (string filename, string mimeType) {
51 this.uri = StringFu.PathToQuotedFileUri (filename);
52 baseStream = new FileStream (filename,
53 FileMode.Open);
54 switch (mimeType) {
55 case "application/zip":
56 baseStream = new ZipInputStream (baseStream);
57 getNextEntry = new GetNextEntryType (GetNextEntryZip);
58 method = "#zip:";
59 break;
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:";
65 break;
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:";
71 break;
72 case "application/x-tar":
73 baseStream = new TarInputStream (baseStream);
74 getNextEntry = new GetNextEntryType (GetNextEntryTar);
75 method = "#tar:";
76 break;
77 case "application/x-gzip":
78 baseStream = new GZipInputStream (baseStream);
79 getNextEntry = new GetNextEntryType (GetNextEntrySingle);
80 method = "#gzip:";
81 break;
82 case "application/x-bzip":
83 baseStream = new BZip2InputStream (baseStream);
84 getNextEntry = new GetNextEntryType (GetNextEntrySingle);
85 method = "#bzip:";
86 break;
87 default:
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();
100 if (entry != null)
101 return uri + method + entry.Name;
102 else
103 return null;
106 public string GetNextEntrySingle () {
107 if (first) {
108 first = false;
109 return uri + method;
110 } else
111 return null;
114 public string GetNextEntryTar () {
115 TarInputStream inputStream = baseStream as TarInputStream;
116 TarEntry entry = inputStream.GetNextEntry();
117 if (entry != null)
118 return uri + method + entry.Name;
119 else
120 return null;
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 {
150 get {
151 return baseStream.CanRead;
154 public override bool CanSeek {
155 get {
156 return false;
159 public override bool CanWrite {
160 get {
161 return false;
164 public override long Length {
165 get {
166 throw new System.NotSupportedException();
168 set {
169 throw new System.NotSupportedException();
172 public override long Position {
173 get {
174 throw new System.NotSupportedException();
176 set {
177 throw new System.NotSupportedException();