2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-09-05 17:37:31 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : GZip.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Filters.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Allow to open files that are compressed using gzip.
|
|
|
|
|
//
|
|
|
|
|
// --[ 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
|
2016-09-05 17:37:31 +01:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
|
|
|
|
|
namespace DiscImageChef.Filters
|
|
|
|
|
{
|
2017-12-23 04:06:02 +00:00
|
|
|
/// <summary>
|
2017-12-24 02:43:49 +00:00
|
|
|
/// Decompress gzip files while reading
|
2017-12-23 04:06:02 +00:00
|
|
|
/// </summary>
|
2017-12-26 06:05:12 +00:00
|
|
|
public class GZip : IFilter
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
string basePath;
|
2016-09-05 17:37:31 +01:00
|
|
|
DateTime creationTime;
|
2018-06-22 08:08:38 +01:00
|
|
|
Stream dataStream;
|
|
|
|
|
uint decompressedSize;
|
2017-12-24 02:43:49 +00:00
|
|
|
DateTime lastWriteTime;
|
2018-06-22 08:08:38 +01:00
|
|
|
bool opened;
|
|
|
|
|
Stream zStream;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string Name => "GZip";
|
2018-06-22 08:08:38 +01:00
|
|
|
public Guid Id => new Guid("F4996661-4A29-42C9-A2C7-3904EF40F3B0");
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Close()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2017-12-22 16:17:40 +00:00
|
|
|
dataStream?.Close();
|
2016-09-05 17:37:31 +01:00
|
|
|
dataStream = null;
|
2018-06-22 08:08:38 +01:00
|
|
|
basePath = null;
|
|
|
|
|
opened = false;
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string GetBasePath()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return basePath;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public Stream GetDataForkStream()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2017-06-07 18:23:30 +01:00
|
|
|
return zStream;
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string GetPath()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return basePath;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public Stream GetResourceForkStream()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool HasResourceFork()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool Identify(byte[] buffer)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return buffer[0] == 0x1F && buffer[1] == 0x8B && buffer[2] == 0x08;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool Identify(Stream stream)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[3];
|
|
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
stream.Read(buffer, 0, 3);
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
|
|
return buffer[0] == 0x1F && buffer[1] == 0x8B && buffer[2] == 0x08;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool Identify(string path)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2017-12-21 06:06:19 +00:00
|
|
|
if(!File.Exists(path)) return false;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
2018-06-22 08:08:38 +01:00
|
|
|
byte[] buffer = new byte[3];
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
stream.Read(buffer, 0, 3);
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
return buffer[0] == 0x1F && buffer[1] == 0x8B && buffer[2] == 0x08;
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(byte[] buffer)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
byte[] mtime_b = new byte[4];
|
|
|
|
|
byte[] isize_b = new byte[4];
|
|
|
|
|
|
|
|
|
|
dataStream = new MemoryStream(buffer);
|
2018-06-22 08:08:38 +01:00
|
|
|
basePath = null;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
|
|
|
|
dataStream.Seek(4, SeekOrigin.Begin);
|
|
|
|
|
dataStream.Read(mtime_b, 0, 4);
|
|
|
|
|
dataStream.Seek(-4, SeekOrigin.End);
|
|
|
|
|
dataStream.Read(isize_b, 0, 4);
|
|
|
|
|
dataStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2018-06-20 22:22:21 +01:00
|
|
|
uint mtime = BitConverter.ToUInt32(mtime_b, 0);
|
|
|
|
|
uint isize = BitConverter.ToUInt32(isize_b, 0);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
|
|
|
|
decompressedSize = isize;
|
2018-06-22 08:08:38 +01:00
|
|
|
creationTime = DateHandlers.UnixUnsignedToDateTime(mtime);
|
|
|
|
|
lastWriteTime = creationTime;
|
|
|
|
|
zStream =
|
|
|
|
|
new ForcedSeekStream<GZipStream>(decompressedSize, dataStream, CompressionMode.Decompress);
|
2016-09-05 17:37:31 +01:00
|
|
|
opened = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(Stream stream)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
byte[] mtime_b = new byte[4];
|
|
|
|
|
byte[] isize_b = new byte[4];
|
|
|
|
|
|
|
|
|
|
dataStream = stream;
|
2018-06-22 08:08:38 +01:00
|
|
|
basePath = null;
|
2016-09-05 17:37:31 +01:00
|
|
|
|
|
|
|
|
dataStream.Seek(4, SeekOrigin.Begin);
|
|
|
|
|
dataStream.Read(mtime_b, 0, 4);
|
|
|
|
|
dataStream.Seek(-4, SeekOrigin.End);
|
|
|
|
|
dataStream.Read(isize_b, 0, 4);
|
|
|
|
|
dataStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2018-06-20 22:22:21 +01:00
|
|
|
uint mtime = BitConverter.ToUInt32(mtime_b, 0);
|
|
|
|
|
uint isize = BitConverter.ToUInt32(isize_b, 0);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
|
|
|
|
decompressedSize = isize;
|
2018-06-22 08:08:38 +01:00
|
|
|
creationTime = DateHandlers.UnixUnsignedToDateTime(mtime);
|
|
|
|
|
lastWriteTime = creationTime;
|
|
|
|
|
zStream =
|
|
|
|
|
new ForcedSeekStream<GZipStream>(decompressedSize, dataStream, CompressionMode.Decompress);
|
2016-09-05 17:37:31 +01:00
|
|
|
opened = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(string path)
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
byte[] mtime_b = new byte[4];
|
|
|
|
|
byte[] isize_b = new byte[4];
|
|
|
|
|
|
|
|
|
|
dataStream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
2018-06-22 08:08:38 +01:00
|
|
|
basePath = Path.GetFullPath(path);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
|
|
|
|
dataStream.Seek(4, SeekOrigin.Begin);
|
|
|
|
|
dataStream.Read(mtime_b, 0, 4);
|
|
|
|
|
dataStream.Seek(-4, SeekOrigin.End);
|
|
|
|
|
dataStream.Read(isize_b, 0, 4);
|
|
|
|
|
dataStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
2018-06-20 22:22:21 +01:00
|
|
|
uint mtime = BitConverter.ToUInt32(mtime_b, 0);
|
|
|
|
|
uint isize = BitConverter.ToUInt32(isize_b, 0);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
|
|
|
|
decompressedSize = isize;
|
|
|
|
|
FileInfo fi = new FileInfo(path);
|
2018-06-22 08:08:38 +01:00
|
|
|
creationTime = fi.CreationTimeUtc;
|
2017-12-23 03:59:48 +00:00
|
|
|
lastWriteTime = DateHandlers.UnixUnsignedToDateTime(mtime);
|
2018-06-22 08:08:38 +01:00
|
|
|
zStream = new ForcedSeekStream<GZipStream>(decompressedSize, dataStream, CompressionMode.Decompress);
|
|
|
|
|
opened = true;
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public DateTime GetCreationTime()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return creationTime;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public long GetDataForkLength()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return decompressedSize;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public DateTime GetLastWriteTime()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return lastWriteTime;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public long GetLength()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return decompressedSize;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public long GetResourceForkLength()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string GetFilename()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
2017-12-21 17:45:39 +00:00
|
|
|
if(basePath?.EndsWith(".gz", StringComparison.InvariantCultureIgnoreCase) == true)
|
2017-07-01 03:26:31 +01:00
|
|
|
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(".gzip", StringComparison.InvariantCultureIgnoreCase) == true
|
|
|
|
|
? basePath.Substring(0, basePath.Length - 5)
|
|
|
|
|
: basePath;
|
2016-09-05 17:37:31 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string GetParentFolder()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return Path.GetDirectoryName(basePath);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool IsOpened()
|
2016-09-05 17:37:31 +01:00
|
|
|
{
|
|
|
|
|
return opened;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|