123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using Xilium.CefGlue.Common.Handlers;
- namespace fis.Win.Handlers
- {
- internal class ZipHandler : IHostHandler, IDisposable
- {
- private readonly string _fileName = string.Empty;
- private readonly ZipArchive _archive;
- private bool _disposed;
- public string Host { get; }
- public ZipHandler(string host, string zipFile)
- {
- Host = host;
- if (File.Exists(zipFile))
- {
- _fileName = Path.GetFileNameWithoutExtension(zipFile);
- }
- _archive = ZipFile.Open(zipFile, ZipArchiveMode.Read);
- }
- ~ZipHandler()
- {
- Dispose();
- }
- public HandleResult Handle(string path, byte[] postData = null)
- {
- try
- {
- string text = path;
- if (text.StartsWith("/"))
- {
- text = text.Substring(1);
- }
- string path2 = _fileName + "/" + text;
- var entry = _archive.Entries.FirstOrDefault(x => x.FullName == path2);
- if(entry != null)
- {
- string mimeType = MimeTypeMap.GetMimeType(Path.GetExtension(path2)!.ToLower());
- using (var entryStream = entry.Open())
- {
- var ms = new MemoryStream();
- entryStream.CopyTo(ms);
- return new HandleResult(ms, mimeType);
- }
- }
- if (File.Exists(path2))
- {
-
- }
- }
- catch
- {
- }
- return null;
- }
- private void DoDispose()
- {
- if (!_disposed)
- {
- _archive.Dispose();
- _disposed = true;
- }
- }
- public void Dispose()
- {
- // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
- DoDispose();
- GC.SuppressFinalize(this);
- }
- }
- }
|