|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
namespace BookReader
{
[Serializable()]
internal class ZipBook : BaseBook
{
#region -----------------constructors-----------------
public ZipBook(string filePath, bool makeCover) : base( filePath )
{
if( makeCover )
GenerateCover();
}
#endregion
#region -----------------properties-----------------
[NonSerialized()]
private ZipFile _Zip = null;
#endregion
#region -----------------loading/unloading-----------------
override public void Load()
{
_Zip = new ZipFile(base.FilePath);
IEnumerator en = _Zip.GetEnumerator();
while (en.MoveNext())
{
ZipEntry entry = (ZipEntry)en.Current;
if (entry.IsFile)
{
IPage item = (IPage)new ZipPage(en.Current);
Pages.Add(item);
}
}
base.Load();
}
override public void UnLoad()
{
base.UnLoad();
_Zip.Close();
_Zip = null;
}
#endregion
#region -----------------generation of cover page-----------------
private void GenerateCover()
{
Thread t = new Thread(new ParameterizedThreadStart(LoadCoverThread));
t.IsBackground = true;
t.Priority = ThreadPriority.BelowNormal;
t.Start(base.FilePath);
}
private void LoadCoverThread( object o )
{
ZipFile temp = null;
try
{
temp = new ZipFile((string)o);
IEnumerator en = temp.GetEnumerator();
while (en.MoveNext())
{
ZipEntry entry = (ZipEntry)en.Current;
if (entry.IsFile)
{
Stream stream = temp.GetInputStream(entry);
byte[] buff = new byte[stream.Length];
stream.Write(buff, 0, (int)stream.Length);
MemoryStream streamM = new MemoryStream();
streamM.Write(buff, 0, buff.Length);
MemoryStream stream2 = new MemoryStream();
streamM.WriteTo(stream2);
streamM.Flush();
streamM.Close();
stream2.Position = 0;
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
{
BitmapImage myImage = new BitmapImage();
myImage.BeginInit();
myImage.StreamSource = stream2;
myImage.DecodePixelWidth = 70;
myImage.EndInit();
base.Cover = myImage;
});
temp.Close();
temp = null;
streamM = null;
stream2 = null;
return;
}
}
}
catch (Exception err)
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate
{
ExceptionManagement.Manage(err, true);
});
}
finally
{
}
}
#endregion
#region -----------------manage page images-----------------
private BitmapImage GetPageImage(IPage page)
{
return GetImageWithCache(_Zip, page.Tag);
}
override public BitmapImage GetCurrentPageImage()
{
return GetImageWithCache(_Zip, base.CurrentPage.Tag);
}
internal BitmapImage GetImageWithCache(ZipFile zipFile, object tag)
{
if (Cache.Find(tag))
return Cache.GetImage(tag);
else
{
BitmapImage myImageStream = GetImageFromStream(zipFile, tag);
Cache.Store(tag, myImageStream);
return myImageStream;
}
}
private BitmapImage GetImageFromStream(ZipFile zipFile, object Tag)
{
return GetImageFromStream(zipFile, Tag, 0);
}
private BitmapImage GetImageFromStream(ZipFile zipFile, object Tag, int resize)
{
ZipEntry entry = (ZipEntry)Tag;
return StreamToImage.GetImageFromStream(zipFile.GetInputStream(entry), resize);
}
#endregion
#region -----------------cache management-----------------
override public void ManageCache()
{
base.ManageCache();
IPage temp = GetNextPage(CurrentPage);
if( temp != null ) GetImageWithCache(_Zip, temp.Tag);
temp = GetNextPage(temp);
if (temp != null) GetImageWithCache(_Zip, temp.Tag);
temp = GetNextPage(temp);
if (temp != null) GetImageWithCache(_Zip, temp.Tag);
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside
https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/