Files
Aaru/DiscImageChef.Filters/LZip.cs

176 lines
6.2 KiB
C#
Raw Normal View History

2017-06-06 23:04:25 +01:00
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : LZip.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Filters.
2017-06-06 23:04:25 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
2018-12-29 17:34:38 +00:00
// Copyright © 2011-2019 Natalia Portillo
2017-06-06 23:04:25 +01:00
// ****************************************************************************/
2017-06-06 23:04:25 +01:00
using System;
using System.IO;
using DiscImageChef.CommonTypes.Interfaces;
using SharpCompress.Compressors;
using SharpCompress.Compressors.LZMA;
2017-06-06 23:04:25 +01:00
namespace DiscImageChef.Filters
{
/// <summary>
/// Decompress lzip files while reading
/// </summary>
public class LZip : IFilter
2017-06-06 23:04:25 +01:00
{
2018-06-22 08:08:38 +01:00
string basePath;
DateTime creationTime;
2018-06-22 08:08:38 +01:00
Stream dataStream;
long decompressedSize;
Stream innerStream;
DateTime lastWriteTime;
2018-06-22 08:08:38 +01:00
bool opened;
2018-08-29 22:15:43 +01:00
public string Name => "LZip";
public Guid Id => new Guid("09D715E9-20C0-48B1-A8D9-D8897CEC57C9");
public string Author => "Natalia Portillo";
public void Close()
{
dataStream?.Close();
dataStream = null;
2018-06-22 08:08:38 +01:00
basePath = null;
opened = false;
}
2018-08-29 22:15:43 +01:00
public string GetBasePath() => basePath;
2018-08-29 22:15:43 +01:00
public Stream GetDataForkStream() => innerStream;
2018-08-29 22:15:43 +01:00
public string GetPath() => basePath;
2018-08-29 22:15:43 +01:00
public Stream GetResourceForkStream() => null;
2018-08-29 22:15:43 +01:00
public bool HasResourceFork() => false;
2018-08-29 22:15:43 +01:00
public bool Identify(byte[] buffer) =>
buffer[0] == 0x4C && buffer[1] == 0x5A && buffer[2] == 0x49 && buffer[3] == 0x50 && buffer[4] == 0x01;
public bool Identify(Stream stream)
{
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;
}
public bool Identify(string path)
{
if(!File.Exists(path)) return false;
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
2018-06-22 08:08:38 +01:00
byte[] buffer = new byte[5];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(buffer, 0, 5);
stream.Seek(0, SeekOrigin.Begin);
return buffer[0] == 0x4C && buffer[1] == 0x5A && buffer[2] == 0x49 && buffer[3] == 0x50 &&
buffer[4] == 0x01;
}
public void Open(byte[] buffer)
{
2018-06-22 08:08:38 +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 =
2019-02-12 18:21:06 +00:00
new ForcedSeekStream<LZipStream>(decompressedSize, dataStream, CompressionMode.Decompress);
opened = true;
}
public void Open(Stream stream)
{
2018-06-22 08:08:38 +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 =
2019-02-12 18:21:06 +00:00
new ForcedSeekStream<LZipStream>(decompressedSize, dataStream, CompressionMode.Decompress);
opened = true;
}
public void Open(string path)
{
dataStream = new FileStream(path, FileMode.Open, FileAccess.Read);
2018-06-22 08:08:38 +01:00
basePath = Path.GetFullPath(path);
FileInfo fi = new FileInfo(path);
2018-06-22 08:08:38 +01:00
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 =
2019-02-12 18:21:06 +00:00
new ForcedSeekStream<LZipStream>(decompressedSize, dataStream, CompressionMode.Decompress);
opened = true;
}
2018-08-29 22:15:43 +01:00
public DateTime GetCreationTime() => creationTime;
2018-08-29 22:15:43 +01:00
public long GetDataForkLength() => decompressedSize;
2018-08-29 22:15:43 +01:00
public DateTime GetLastWriteTime() => lastWriteTime;
2018-08-29 22:15:43 +01:00
public long GetLength() => decompressedSize;
2018-08-29 22:15:43 +01:00
public long GetResourceForkLength() => 0;
public string GetFilename()
{
if(basePath?.EndsWith(".lz", StringComparison.InvariantCultureIgnoreCase) == true)
return basePath.Substring(0, basePath.Length - 3);
2017-12-19 20:33:03 +00:00
2018-06-20 22:22:21 +01:00
return basePath?.EndsWith(".lzip", StringComparison.InvariantCultureIgnoreCase) == true
? basePath.Substring(0, basePath.Length - 5)
: basePath;
}
2018-08-29 22:15:43 +01:00
public string GetParentFolder() => Path.GetDirectoryName(basePath);
2018-08-29 22:15:43 +01:00
public bool IsOpened() => opened;
2017-06-06 23:04:25 +01:00
}
}