2017-06-06 23:04:25 +01:00
|
|
|
|
// /***************************************************************************
|
|
|
|
|
|
// The Disc Image Chef
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : LZip.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
2017-06-07 18:36:06 +01:00
|
|
|
|
// Component : Filters.
|
2017-06-06 23:04:25 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-06-07 18:36:06 +01:00
|
|
|
|
// Allow to open files that are compressed using lzip.
|
2017-06-06 23:04:25 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2017-06-06 23:04:25 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-06-06 23:04:25 +01:00
|
|
|
|
using System;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using SharpCompress.Compressors;
|
|
|
|
|
|
using SharpCompress.Compressors.LZMA;
|
|
|
|
|
|
|
2017-06-06 23:04:25 +01:00
|
|
|
|
namespace DiscImageChef.Filters
|
|
|
|
|
|
{
|
2017-12-23 04:06:02 +00:00
|
|
|
|
/// <summary>
|
2017-12-24 02:43:49 +00:00
|
|
|
|
/// Decompress lzip files while reading
|
2017-12-23 04:06:02 +00:00
|
|
|
|
/// </summary>
|
2017-12-26 06:05:12 +00:00
|
|
|
|
public class LZip : IFilter
|
2017-06-06 23:04:25 +01:00
|
|
|
|
{
|
2017-06-07 18:36:06 +01:00
|
|
|
|
string basePath;
|
|
|
|
|
|
DateTime creationTime;
|
2017-12-24 02:43:49 +00:00
|
|
|
|
Stream dataStream;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
long decompressedSize;
|
|
|
|
|
|
Stream innerStream;
|
2017-12-24 02:43:49 +00:00
|
|
|
|
DateTime lastWriteTime;
|
|
|
|
|
|
bool opened;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public string Name => "LZip";
|
|
|
|
|
|
public Guid Id => new Guid("09D715E9-20C0-48B1-A8D9-D8897CEC57C9");
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public void Close()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2017-12-22 16:17:40 +00:00
|
|
|
|
dataStream?.Close();
|
2017-06-07 18:36:06 +01:00
|
|
|
|
dataStream = null;
|
|
|
|
|
|
basePath = null;
|
|
|
|
|
|
opened = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public string GetBasePath()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return basePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public Stream GetDataForkStream()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return innerStream;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public string GetPath()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return basePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public Stream GetResourceForkStream()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool HasResourceFork()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(byte[] buffer)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2017-12-19 20:33:03 +00:00
|
|
|
|
return buffer[0] == 0x4C && buffer[1] == 0x5A && buffer[2] == 0x49 && buffer[3] == 0x50 &&
|
|
|
|
|
|
buffer[4] == 0x01;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(Stream stream)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
byte[] buffer = new byte[5];
|
|
|
|
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
stream.Read(buffer, 0, 5);
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
|
return buffer[0] == 0x4C && buffer[1] == 0x5A && buffer[2] == 0x49 && buffer[3] == 0x50 &&
|
|
|
|
|
|
buffer[4] == 0x01;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool Identify(string path)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2017-12-21 06:06:19 +00:00
|
|
|
|
if(!File.Exists(path)) return false;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
byte[] buffer = new byte[5];
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
stream.Read(buffer, 0, 5);
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2017-06-07 18:36:06 +01:00
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
|
return buffer[0] == 0x4C && buffer[1] == 0x5A && buffer[2] == 0x49 && buffer[3] == 0x50 &&
|
|
|
|
|
|
buffer[4] == 0x01;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public void Open(byte[] buffer)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
dataStream = new MemoryStream(buffer);
|
|
|
|
|
|
basePath = null;
|
|
|
|
|
|
creationTime = DateTime.UtcNow;
|
|
|
|
|
|
lastWriteTime = creationTime;
|
2017-06-07 20:03:10 +01:00
|
|
|
|
decompressedSize = BitConverter.ToInt64(buffer, buffer.Length - 16);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
innerStream =
|
|
|
|
|
|
new ForcedSeekStream<LZipStream>(decompressedSize, dataStream, CompressionMode.Decompress, false);
|
2017-06-07 18:36:06 +01:00
|
|
|
|
opened = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public void Open(Stream stream)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
dataStream = stream;
|
|
|
|
|
|
basePath = null;
|
|
|
|
|
|
creationTime = DateTime.UtcNow;
|
|
|
|
|
|
lastWriteTime = creationTime;
|
2017-06-07 20:03:10 +01:00
|
|
|
|
byte[] tmp = new byte[8];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
dataStream.Seek(-16, SeekOrigin.End);
|
2017-06-07 20:03:10 +01:00
|
|
|
|
dataStream.Read(tmp, 0, 8);
|
|
|
|
|
|
decompressedSize = BitConverter.ToInt64(tmp, 0);
|
|
|
|
|
|
dataStream.Seek(0, SeekOrigin.Begin);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
innerStream =
|
|
|
|
|
|
new ForcedSeekStream<LZipStream>(decompressedSize, dataStream, CompressionMode.Decompress, false);
|
2017-06-07 18:36:06 +01:00
|
|
|
|
opened = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public void Open(string path)
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
dataStream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
basePath = Path.GetFullPath(path);
|
|
|
|
|
|
|
|
|
|
|
|
DateTime start = DateTime.UtcNow;
|
|
|
|
|
|
DateTime end = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
|
|
FileInfo fi = new FileInfo(path);
|
|
|
|
|
|
creationTime = fi.CreationTimeUtc;
|
|
|
|
|
|
lastWriteTime = fi.LastWriteTimeUtc;
|
2017-06-07 20:03:10 +01:00
|
|
|
|
byte[] tmp = new byte[8];
|
|
|
|
|
|
dataStream.Seek(-16, SeekOrigin.End);
|
|
|
|
|
|
dataStream.Read(tmp, 0, 8);
|
|
|
|
|
|
decompressedSize = BitConverter.ToInt64(tmp, 0);
|
|
|
|
|
|
dataStream.Seek(0, SeekOrigin.Begin);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
innerStream =
|
|
|
|
|
|
new ForcedSeekStream<LZipStream>(decompressedSize, dataStream, CompressionMode.Decompress, false);
|
2017-06-07 18:36:06 +01:00
|
|
|
|
opened = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public DateTime GetCreationTime()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return creationTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public long GetDataForkLength()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return decompressedSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public DateTime GetLastWriteTime()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return lastWriteTime;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public long GetLength()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return decompressedSize;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public long GetResourceForkLength()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public string GetFilename()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
2017-12-21 17:45:39 +00:00
|
|
|
|
if(basePath?.EndsWith(".lz", StringComparison.InvariantCultureIgnoreCase) == true)
|
2017-07-01 03:26:31 +01:00
|
|
|
|
return basePath.Substring(0, basePath.Length - 3);
|
2017-12-21 17:45:39 +00:00
|
|
|
|
if(basePath?.EndsWith(".lzip", StringComparison.InvariantCultureIgnoreCase) == true)
|
2017-07-01 03:26:31 +01:00
|
|
|
|
return basePath.Substring(0, basePath.Length - 5);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
2017-07-01 03:26:31 +01:00
|
|
|
|
return basePath;
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public string GetParentFolder()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return Path.GetDirectoryName(basePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
|
public bool IsOpened()
|
2017-06-07 18:36:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
return opened;
|
2017-06-06 23:04:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-06-07 18:36:06 +01:00
|
|
|
|
}
|