Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ZipCodec.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
6
7namespace Waher.Content.Zip
8{
13 {
17 public ZipCodec()
18 {
19 }
20
24 public const string DefaultContentType = "application/zip";
25
29 public static readonly string[] ZipFileContentTypes = new string[]
30 {
32 "application/x-zip-compressed"
33 };
34
38 public static readonly string[] ZipFileFileExtensions = new string[]
39 {
40 "zip"
41 };
42
47
52
59 public bool Decodes(string ContentType, out Grade Grade)
60 {
61 if (Array.IndexOf(ZipFileContentTypes, ContentType) >= 0)
62 {
63 Grade = Grade.Barely;
64 return true;
65 }
66 else
67 {
68 Grade = Grade.NotAtAll;
69 return false;
70 }
71 }
72
83 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
84 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
85 {
86 return Task.FromResult(new ContentResponse(ContentType, new ZipFile(Data), Data));
87 }
88
95 public bool TryGetContentType(string FileExtension, out string ContentType)
96 {
97 switch (FileExtension.ToLower())
98 {
99 case "zip":
100 ContentType = DefaultContentType;
101 return true;
102
103 default:
104 ContentType = string.Empty;
105 return false;
106 }
107 }
108
115 public bool TryGetFileExtension(string ContentType, out string FileExtension)
116 {
117 switch (ContentType.ToLower())
118 {
120 FileExtension = ZipFileFileExtensions[0];
121 return true;
122
123 default:
124 FileExtension = string.Empty;
125 return false;
126 }
127 }
128
136 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
137 {
138 if (Object is ZipFile)
139 {
140 if (InternetContent.IsAccepted(ZipFileContentTypes, AcceptedContentTypes))
141 {
142 Grade = Grade.Barely;
143 return true;
144 }
145 }
146
147 Grade = Grade.NotAtAll;
148 return false;
149 }
150
159 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
160 {
161 if (!InternetContent.IsAccepted(ZipFileContentTypes, out string ContentType, AcceptedContentTypes))
162 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object, or content type not accepted.", nameof(Object))));
163
164 byte[] Data;
165
166 if (Object is ZipFile ZipFile)
167 Data = ZipFile.Binary;
168 else
169 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object of type " + (Object?.GetType()?.FullName) + ".", nameof(Object))));
170
171 return Task.FromResult(new ContentResponse(ContentType, Object, Data));
172 }
173 }
174}
Contains information about a response to a content request.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
ZIP File encoder/decoder.
Definition: ZipCodec.cs:13
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of an item, given its Content-Type.
Definition: ZipCodec.cs:115
static readonly string[] ZipFileContentTypes
ZIP File content types.
Definition: ZipCodec.cs:29
string[] ContentTypes
Supported content types.
Definition: ZipCodec.cs:46
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes an object with a given content type.
Definition: ZipCodec.cs:59
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a given object.
Definition: ZipCodec.cs:136
const string DefaultContentType
application/zip
Definition: ZipCodec.cs:24
static readonly string[] ZipFileFileExtensions
Plain text file extensions.
Definition: ZipCodec.cs:38
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of an item, given its file extension.
Definition: ZipCodec.cs:95
ZipCodec()
ZIP File encoder/decoder.
Definition: ZipCodec.cs:17
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object.
Definition: ZipCodec.cs:83
string[] FileExtensions
Supported file extensions.
Definition: ZipCodec.cs:51
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object.
Definition: ZipCodec.cs:159
Encapsulates a ZIP File
Definition: ZipFile.cs:7
byte[] Binary
Binary representation of ZIP file.
Definition: ZipFile.cs:22
Interface for reporting progress about an encoding or decoding.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7