|
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using SevenZip;
namespace BookReader
{
internal class RarBook : BaseBook
{
#region -----------------constructors-----------------
public RarBook(string filePath, bool makeCover) : base( filePath )
{
try
{
if (!_IsInitDone)
{
string sevenZip = Assembly.GetExecutingAssembly().Location.Replace("BookReader.exe", "Dependencies\\7z.dll");
SevenZipExtractor.SetLibraryPath(sevenZip);
_IsInitDone = true;
}
}
catch (SevenZipLibraryException err)
{
ExceptionManagement.Manage(err, false);
}
if (makeCover)
GenerateCover();
}
#endregion
#region -----------------properties-----------------
private static bool _IsInitDone = false;
private SevenZipExtractor _Rar = null;
#endregion
#region -----------------loading/unloading-----------------
override public void Load()
{
_Rar = new SevenZipExtractor(base.FilePath);
foreach (ArchiveFileInfo fil in _Rar.ArchiveFileData)
{
if (!fil.IsDirectory)
{
if (Properties.Settings.Default.ImageFilter.Contains(new FileInfo(fil.FileName).Extension.ToUpper()))
{
RarPage item = new RarPage(fil.FileName);
Pages.Add(item);
}
}
}
base.Load();
}
override public void UnLoad()
{
base.UnLoad();
_Rar.Dispose();
_Rar = 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)
{
SevenZipExtractor temp = null;
try
{
temp = new SevenZipExtractor((string)o);
foreach (ArchiveFileInfo fil in temp.ArchiveFileData)
{
if (!fil.IsDirectory && Properties.Settings.Default.ImageFilter.Contains(new FileInfo(fil.FileName).Extension.ToUpper()))
{
MemoryStream stream = new MemoryStream();
temp.ExtractFile(fil.FileName, stream);
MemoryStream stream2 = new MemoryStream();
stream.WriteTo(stream2);
stream.Flush();
stream.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;
});
stream = null;
stream2 = null;
return;
}
}
}
catch (Exception err)
{
ExceptionManagement.Manage(err, true);
}
finally
{
temp.Dispose();
temp = null;
}
}
#endregion
#region -----------------manage page images-----------------
internal BitmapImage GetPageImage(IBookItem page )
{
return GetImageWithCache(_Rar, page.FilePath);
}
override public BitmapImage GetCurrentPageImage()
{
return GetImageWithCache(_Rar, base.CurrentPage.FilePath);
}
private BitmapImage GetImageWithCache(SevenZipExtractor zipFile, string fileName)
{
if (Cache.Find(fileName))
return Cache.GetImage(fileName);
else
{
BitmapImage myImageStream = GetImageFromStream(zipFile, fileName);
Cache.Store(fileName, myImageStream);
return myImageStream;
}
}
private BitmapImage GetImageFromStream(SevenZipExtractor zipFile, string fileName)
{
return GetImageFromStream(zipFile, fileName, 0);
}
private BitmapImage GetImageFromStream(SevenZipExtractor zipFile, string fileName, int resize)
{
MemoryStream stream = new MemoryStream();
zipFile.ExtractFile(fileName, stream);
return StreamToImage.GetImageFromStreamBug(stream, resize);
}
#endregion
#region -----------------cache management-----------------
override public void ManageCache()
{
base.ManageCache();
IBookItem temp = GetNextPage(CurrentPage);
if( temp != null ) GetImageWithCache(_Rar, temp.FilePath);
temp = GetNextPage(temp);
if (temp != null) GetImageWithCache(_Rar, temp.FilePath);
temp = GetNextPage(temp);
if (temp != null) GetImageWithCache(_Rar, temp.FilePath);
}
#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/