2008-11-10 08:42:42 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
2010-02-08 01:29:31 +00:00
|
|
|
using CUETools.Compression;
|
2008-11-10 08:42:42 +00:00
|
|
|
|
|
|
|
|
|
2012-04-08 23:54:36 +00:00
|
|
|
/* Author: Grigory Chudov
|
2008-11-10 08:42:42 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2010-02-08 01:29:31 +00:00
|
|
|
namespace CUETools.Compression.Rar
|
2008-11-10 08:42:42 +00:00
|
|
|
{
|
|
|
|
|
public class RarStream : Stream
|
|
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
public RarStream(string path, string fileName)
|
2008-11-10 08:42:42 +00:00
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
_close = false;
|
|
|
|
|
_eof = false;
|
|
|
|
|
_rewind = false;
|
2008-11-10 08:42:42 +00:00
|
|
|
_unrar = new Unrar();
|
|
|
|
|
_buffer = null;
|
|
|
|
|
_offset = 0;
|
|
|
|
|
_length = 0;
|
2008-11-10 09:31:14 +00:00
|
|
|
_pos = 0;
|
2008-11-10 12:39:07 +00:00
|
|
|
_path = path;
|
|
|
|
|
_fileName = fileName;
|
2008-11-12 05:45:48 +00:00
|
|
|
_workThread = null;
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
|
|
|
|
public override bool CanRead
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
public override bool CanSeek
|
|
|
|
|
{
|
2008-11-10 09:31:14 +00:00
|
|
|
get { return true; }
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
|
|
|
|
public override bool CanWrite
|
|
|
|
|
{
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
|
|
|
|
public override long Length
|
|
|
|
|
{
|
2008-11-10 09:31:14 +00:00
|
|
|
get
|
|
|
|
|
{
|
2008-11-12 05:45:48 +00:00
|
|
|
Go();
|
2008-11-10 09:31:14 +00:00
|
|
|
lock (this)
|
|
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
while (_size == null && !_close)
|
2008-11-10 09:31:14 +00:00
|
|
|
Monitor.Wait(this);
|
|
|
|
|
}
|
2008-11-17 07:54:31 +00:00
|
|
|
if (_close)
|
|
|
|
|
throw new IOException("Decompression failed", _ex);
|
2008-11-10 09:31:14 +00:00
|
|
|
return _size.Value;
|
|
|
|
|
}
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
|
|
|
|
public override long Position
|
|
|
|
|
{
|
2008-11-10 10:31:35 +00:00
|
|
|
get { return _seek_to == null ? _pos : _seek_to.Value; }
|
2008-11-10 08:42:42 +00:00
|
|
|
set { Seek(value, SeekOrigin.Begin); }
|
|
|
|
|
}
|
|
|
|
|
public override void Close()
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
_close = true;
|
2008-11-10 08:42:42 +00:00
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
}
|
2008-11-10 09:31:14 +00:00
|
|
|
if (_workThread != null)
|
|
|
|
|
{
|
|
|
|
|
_workThread.Join();
|
|
|
|
|
_workThread = null;
|
|
|
|
|
}
|
|
|
|
|
if (_unrar != null)
|
|
|
|
|
{
|
|
|
|
|
_unrar.Close();
|
|
|
|
|
_unrar = null;
|
|
|
|
|
}
|
2008-11-10 08:42:42 +00:00
|
|
|
base.Close();
|
|
|
|
|
}
|
|
|
|
|
public override void Flush()
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
public override void SetLength(long value)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
public override int Read(byte[] array, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
int total = 0;
|
2008-11-12 05:45:48 +00:00
|
|
|
Go();
|
2008-11-10 08:42:42 +00:00
|
|
|
while (count > 0)
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
2008-11-12 05:45:48 +00:00
|
|
|
while (_buffer == null && !_eof && !_close)
|
2008-11-10 08:42:42 +00:00
|
|
|
Monitor.Wait(this);
|
2008-11-12 05:45:48 +00:00
|
|
|
if (_close)
|
2009-02-22 07:47:56 +00:00
|
|
|
throw _ex ?? new IOException("Decompression failed");
|
2008-11-10 08:42:42 +00:00
|
|
|
if (_buffer == null)
|
|
|
|
|
return total;
|
2008-11-10 09:31:14 +00:00
|
|
|
if (_seek_to != null)
|
|
|
|
|
{
|
|
|
|
|
if (_seek_to.Value < _pos)
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
if (_length <= _seek_to.Value - _pos)
|
|
|
|
|
{
|
|
|
|
|
_pos += _length;
|
|
|
|
|
_buffer = null;
|
|
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
_offset += (int)(_seek_to.Value - _pos);
|
|
|
|
|
_length -= (int)(_seek_to.Value - _pos);
|
|
|
|
|
_pos = _seek_to.Value;
|
|
|
|
|
_seek_to = null;
|
|
|
|
|
}
|
2008-11-10 08:42:42 +00:00
|
|
|
if (_length > count)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(_buffer, _offset, array, offset, count);
|
|
|
|
|
total += count;
|
2008-11-10 09:31:14 +00:00
|
|
|
_pos += count;
|
2008-11-10 08:42:42 +00:00
|
|
|
_offset += count;
|
|
|
|
|
_length -= count;
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
Array.Copy(_buffer, _offset, array, offset, _length);
|
|
|
|
|
total += _length;
|
2008-11-10 09:31:14 +00:00
|
|
|
_pos += _length;
|
2008-11-10 08:42:42 +00:00
|
|
|
offset += _length;
|
|
|
|
|
count -= _length;
|
|
|
|
|
_buffer = null;
|
|
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return total;
|
|
|
|
|
}
|
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
{
|
2008-11-10 10:31:35 +00:00
|
|
|
switch (origin)
|
|
|
|
|
{
|
|
|
|
|
case SeekOrigin.Begin:
|
|
|
|
|
_seek_to = offset;
|
|
|
|
|
break;
|
|
|
|
|
case SeekOrigin.Current:
|
|
|
|
|
_seek_to = Position + offset;
|
|
|
|
|
break;
|
|
|
|
|
case SeekOrigin.End:
|
2008-11-17 07:54:31 +00:00
|
|
|
_seek_to = Length + offset;
|
2008-11-10 10:31:35 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2008-11-17 07:54:31 +00:00
|
|
|
if (_seek_to.Value > Length)
|
|
|
|
|
{
|
|
|
|
|
_seek_to = null;
|
|
|
|
|
throw new IOException("Invalid seek");
|
|
|
|
|
}
|
2008-11-10 10:31:35 +00:00
|
|
|
if (_seek_to.Value == _pos)
|
|
|
|
|
{
|
|
|
|
|
_seek_to = null;
|
|
|
|
|
return _pos;
|
|
|
|
|
}
|
|
|
|
|
if (_seek_to.Value < _pos)
|
|
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
_pos = 0;
|
|
|
|
|
_rewind = true;
|
|
|
|
|
_buffer = null;
|
|
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
}
|
2008-11-10 10:31:35 +00:00
|
|
|
}
|
|
|
|
|
return _seek_to.Value;
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
|
|
|
|
public override void Write(byte[] array, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
2010-02-08 01:29:31 +00:00
|
|
|
public event EventHandler<CompressionPasswordRequiredEventArgs> PasswordRequired;
|
|
|
|
|
public event EventHandler<CompressionExtractionProgressEventArgs> ExtractionProgress;
|
2008-11-10 08:42:42 +00:00
|
|
|
|
|
|
|
|
private Unrar _unrar;
|
|
|
|
|
private string _fileName;
|
|
|
|
|
private Thread _workThread;
|
2008-11-10 12:39:07 +00:00
|
|
|
private bool _close, _rewind, _eof;
|
2008-11-10 08:42:42 +00:00
|
|
|
private byte[] _buffer;
|
2008-11-12 05:45:48 +00:00
|
|
|
private Exception _ex;
|
2008-11-10 08:42:42 +00:00
|
|
|
int _offset, _length;
|
2008-11-10 09:31:14 +00:00
|
|
|
long? _size;
|
|
|
|
|
long? _seek_to;
|
|
|
|
|
long _pos;
|
2008-11-10 12:39:07 +00:00
|
|
|
string _path;
|
2008-11-10 08:42:42 +00:00
|
|
|
|
2008-11-12 05:45:48 +00:00
|
|
|
private void Go()
|
2008-11-10 08:42:42 +00:00
|
|
|
{
|
2008-11-12 05:45:48 +00:00
|
|
|
if (_workThread != null) return;
|
|
|
|
|
_workThread = new Thread(Decompress);
|
|
|
|
|
_workThread.Priority = ThreadPriority.BelowNormal;
|
|
|
|
|
_workThread.IsBackground = true;
|
|
|
|
|
_workThread.Start(null);
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void unrar_DataAvailable(object sender, DataAvailableEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
while (_buffer != null && !_close)
|
2008-11-10 08:42:42 +00:00
|
|
|
Monitor.Wait(this);
|
2008-11-10 12:39:07 +00:00
|
|
|
if (_close)
|
|
|
|
|
{
|
|
|
|
|
e.ContinueOperation = false;
|
|
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (_rewind)
|
2008-11-10 08:42:42 +00:00
|
|
|
{
|
|
|
|
|
e.ContinueOperation = false;
|
|
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_buffer = e.Data;
|
|
|
|
|
_length = _buffer.Length;
|
|
|
|
|
_offset = 0;
|
|
|
|
|
e.ContinueOperation = true;
|
|
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Decompress(object o)
|
|
|
|
|
{
|
2008-11-12 05:45:48 +00:00
|
|
|
_unrar.DataAvailable += new DataAvailableHandler(unrar_DataAvailable);
|
|
|
|
|
_unrar.PasswordRequired += PasswordRequired;
|
|
|
|
|
_unrar.ExtractionProgress += ExtractionProgress;
|
2008-11-18 14:48:26 +00:00
|
|
|
#if !DEBUG
|
2008-11-12 05:45:48 +00:00
|
|
|
try
|
2008-11-18 14:48:26 +00:00
|
|
|
#endif
|
2008-11-10 08:42:42 +00:00
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
do
|
2008-11-10 08:42:42 +00:00
|
|
|
{
|
2008-11-17 07:54:31 +00:00
|
|
|
bool foundFile = false;
|
2008-11-10 12:39:07 +00:00
|
|
|
_unrar.Open(_path, Unrar.OpenMode.Extract);
|
|
|
|
|
while (_unrar.ReadHeader())
|
2008-11-10 08:42:42 +00:00
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
if (_unrar.CurrentFile.FileName == _fileName)
|
2008-11-10 08:42:42 +00:00
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
lock (this)
|
|
|
|
|
{
|
|
|
|
|
if (_size == null)
|
|
|
|
|
{
|
|
|
|
|
_size = _unrar.CurrentFile.UnpackedSize;
|
|
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_unrar.Test();
|
2008-11-17 07:54:31 +00:00
|
|
|
foundFile = true;
|
2008-11-10 12:39:07 +00:00
|
|
|
break;
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
2008-11-10 12:39:07 +00:00
|
|
|
else
|
|
|
|
|
_unrar.Skip();
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
2008-11-10 12:39:07 +00:00
|
|
|
_unrar.Close();
|
|
|
|
|
lock (this)
|
|
|
|
|
{
|
2008-11-17 07:54:31 +00:00
|
|
|
if (!foundFile)
|
|
|
|
|
{
|
|
|
|
|
_ex = new FileNotFoundException();
|
2008-11-10 12:39:07 +00:00
|
|
|
break;
|
2008-11-17 07:54:31 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_eof = true;
|
|
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
while (!_rewind && !_close)
|
|
|
|
|
Monitor.Wait(this);
|
|
|
|
|
if (_close)
|
|
|
|
|
break;
|
|
|
|
|
_rewind = false;
|
|
|
|
|
_eof = false;
|
|
|
|
|
}
|
2008-11-10 12:39:07 +00:00
|
|
|
}
|
|
|
|
|
} while (true);
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
2008-11-18 14:48:26 +00:00
|
|
|
#if !DEBUG
|
2008-11-12 05:45:48 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_ex = ex;
|
|
|
|
|
}
|
2008-11-18 14:48:26 +00:00
|
|
|
#endif
|
2008-11-10 09:31:14 +00:00
|
|
|
lock (this)
|
|
|
|
|
{
|
2008-11-10 12:39:07 +00:00
|
|
|
_close = true;
|
2008-11-10 09:31:14 +00:00
|
|
|
Monitor.Pulse(this);
|
|
|
|
|
}
|
2008-11-10 08:42:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|