2016-03-05 21:25:04 +08:00
|
|
|
//! \file ArcZIP.cs
|
|
|
|
//! \date Sat Mar 05 14:47:42 2016
|
|
|
|
//! \brief PKWARE ZIP archive format.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2016 by morkt
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO.Compression;
|
|
|
|
using System.Linq;
|
2016-03-05 23:05:47 +08:00
|
|
|
using System.Text;
|
2016-03-06 07:28:58 +08:00
|
|
|
using GameRes.Formats.Strings;
|
2016-03-05 21:25:04 +08:00
|
|
|
|
2018-01-29 07:49:43 +08:00
|
|
|
using SharpZip = ICSharpCode.SharpZipLib.Zip;
|
|
|
|
|
2016-03-05 21:25:04 +08:00
|
|
|
namespace GameRes.Formats.PkWare
|
|
|
|
{
|
|
|
|
internal class ZipEntry : PackedEntry
|
|
|
|
{
|
2018-01-29 07:49:43 +08:00
|
|
|
public readonly SharpZip.ZipEntry NativeEntry;
|
2016-03-05 21:25:04 +08:00
|
|
|
|
2018-01-29 07:49:43 +08:00
|
|
|
public ZipEntry (SharpZip.ZipEntry zip_entry)
|
2016-03-05 21:25:04 +08:00
|
|
|
{
|
|
|
|
NativeEntry = zip_entry;
|
2018-01-29 07:49:43 +08:00
|
|
|
Name = zip_entry.Name;
|
|
|
|
Type = FormatCatalog.Instance.GetTypeFromName (zip_entry.Name);
|
2016-03-05 21:25:04 +08:00
|
|
|
IsPacked = true;
|
|
|
|
// design decision of having 32bit entry sizes was made early during GameRes
|
|
|
|
// library development. nevertheless, large files will be extracted correctly
|
|
|
|
// despite the fact that size is reported as uint.MaxValue, because extraction is
|
|
|
|
// performed by .Net framework based on real size value.
|
2018-01-29 07:49:43 +08:00
|
|
|
Size = (uint)Math.Min (zip_entry.CompressedSize, uint.MaxValue);
|
|
|
|
UnpackedSize = (uint)Math.Min (zip_entry.Size, uint.MaxValue);
|
|
|
|
Offset = zip_entry.Offset;
|
2016-03-05 21:25:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class PkZipArchive : ArcFile
|
|
|
|
{
|
2018-01-29 07:49:43 +08:00
|
|
|
readonly SharpZip.ZipFile m_zip;
|
|
|
|
|
|
|
|
public SharpZip.ZipFile Native { get { return m_zip; } }
|
2016-03-05 21:25:04 +08:00
|
|
|
|
2018-01-29 07:49:43 +08:00
|
|
|
public PkZipArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, SharpZip.ZipFile native)
|
2016-03-05 21:25:04 +08:00
|
|
|
: base (arc, impl, dir)
|
|
|
|
{
|
|
|
|
m_zip = native;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IDisposable implementation
|
|
|
|
bool _zip_disposed = false;
|
|
|
|
protected override void Dispose (bool disposing)
|
|
|
|
{
|
2016-03-14 02:20:08 +08:00
|
|
|
if (!_zip_disposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
2018-01-29 07:49:43 +08:00
|
|
|
m_zip.Close();
|
2016-03-14 02:20:08 +08:00
|
|
|
_zip_disposed = true;
|
|
|
|
}
|
|
|
|
base.Dispose (disposing);
|
2016-03-05 21:25:04 +08:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|
2018-01-30 10:34:24 +08:00
|
|
|
[Serializable]
|
|
|
|
public class ZipScheme : ResourceScheme
|
|
|
|
{
|
|
|
|
public Dictionary<string, string> KnownKeys;
|
|
|
|
}
|
|
|
|
|
2016-03-05 21:25:04 +08:00
|
|
|
[Export(typeof(ArchiveFormat))]
|
|
|
|
public class ZipOpener : ArchiveFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "ZIP"; } }
|
|
|
|
public override string Description { get { return "PKWARE archive format"; } }
|
|
|
|
public override uint Signature { get { return 0; } }
|
|
|
|
public override bool IsHierarchic { get { return true; } }
|
2016-10-11 04:05:22 +08:00
|
|
|
public override bool CanWrite { get { return true; } }
|
2016-03-05 21:25:04 +08:00
|
|
|
|
2018-01-22 19:50:29 +08:00
|
|
|
static readonly byte[] PkDirSignature = { (byte)'P', (byte)'K', 5, 6 };
|
|
|
|
|
2018-02-18 07:55:00 +08:00
|
|
|
public ZipOpener ()
|
|
|
|
{
|
|
|
|
Settings = new[] { ZipEncoding };
|
|
|
|
}
|
|
|
|
|
|
|
|
EncodingSetting ZipEncoding = new EncodingSetting ("ZIPEncodingCP");
|
|
|
|
|
2016-03-05 21:25:04 +08:00
|
|
|
public override ArcFile TryOpen (ArcView file)
|
|
|
|
{
|
2018-01-22 19:50:29 +08:00
|
|
|
if (-1 == SearchForSignature (file, PkDirSignature))
|
2016-03-05 21:25:04 +08:00
|
|
|
return null;
|
|
|
|
var input = file.CreateStream();
|
|
|
|
try
|
|
|
|
{
|
2018-01-22 19:50:29 +08:00
|
|
|
return OpenZipArchive (file, input);
|
2016-03-05 21:25:04 +08:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
input.Dispose();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:50:29 +08:00
|
|
|
internal ArcFile OpenZipArchive (ArcView file, Stream input)
|
|
|
|
{
|
2018-01-29 07:49:43 +08:00
|
|
|
SharpZip.ZipConstants.DefaultCodePage = Properties.Settings.Default.ZIPEncodingCP;
|
|
|
|
var zip = new SharpZip.ZipFile (input);
|
2018-01-22 19:50:29 +08:00
|
|
|
try
|
|
|
|
{
|
2018-01-29 07:49:43 +08:00
|
|
|
var files = zip.Cast<SharpZip.ZipEntry>().Where (z => !z.IsDirectory);
|
|
|
|
bool has_encrypted = files.Any (z => z.IsCrypted);
|
|
|
|
if (has_encrypted)
|
|
|
|
zip.Password = QueryPassword (file);
|
|
|
|
var dir = files.Select (z => new ZipEntry (z) as Entry).ToList();
|
2018-01-22 19:50:29 +08:00
|
|
|
return new PkZipArchive (file, this, dir, zip);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2018-01-29 07:49:43 +08:00
|
|
|
zip.Close();
|
2018-01-22 19:50:29 +08:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-05 21:25:04 +08:00
|
|
|
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
|
|
|
{
|
2018-01-29 07:49:43 +08:00
|
|
|
var zarc = (PkZipArchive)arc;
|
|
|
|
var zent = (ZipEntry)entry;
|
|
|
|
return zarc.Native.GetInputStream (zent.NativeEntry);
|
2016-03-05 21:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Search for ZIP 'End of central directory record' near the end of file.
|
|
|
|
/// Returns offset of 'PK' signature or -1 if no signature was found.
|
|
|
|
/// </summary>
|
2018-01-22 19:50:29 +08:00
|
|
|
internal unsafe long SearchForSignature (ArcView file, byte[] signature)
|
2016-03-05 21:25:04 +08:00
|
|
|
{
|
2018-01-22 19:50:29 +08:00
|
|
|
if (signature.Length < 4)
|
|
|
|
throw new ArgumentException ("Invalid ZIP file signature", "signature");
|
|
|
|
|
2016-03-05 21:25:04 +08:00
|
|
|
uint tail_size = (uint)Math.Min (file.MaxOffset, 0x10016L);
|
|
|
|
if (tail_size < 0x16)
|
|
|
|
return -1;
|
|
|
|
var start_offset = file.MaxOffset - tail_size;
|
|
|
|
using (var view = file.CreateViewAccessor (start_offset, tail_size))
|
2018-01-22 19:50:29 +08:00
|
|
|
using (var pointer = new ViewPointer (view, start_offset))
|
2016-03-05 21:25:04 +08:00
|
|
|
{
|
2018-01-22 19:50:29 +08:00
|
|
|
byte* ptr_end = pointer.Value;
|
2016-03-05 21:25:04 +08:00
|
|
|
byte* ptr = ptr_end + tail_size-0x16;
|
2018-01-22 19:50:29 +08:00
|
|
|
for (; ptr >= ptr_end; --ptr)
|
|
|
|
{
|
|
|
|
if (signature[3] == ptr[3] && signature[2] == ptr[2] &&
|
|
|
|
signature[1] == ptr[1] && signature[0] == ptr[0])
|
|
|
|
return start_offset + (ptr-ptr_end);
|
2016-03-05 21:25:04 +08:00
|
|
|
}
|
2018-01-22 19:50:29 +08:00
|
|
|
return -1;
|
2016-03-05 21:25:04 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-05 23:05:47 +08:00
|
|
|
|
2018-01-29 07:49:43 +08:00
|
|
|
string QueryPassword (ArcView file)
|
|
|
|
{
|
2018-01-30 10:34:24 +08:00
|
|
|
var options = Query<ZipOptions> (arcStrings.ZIPEncryptedNotice);
|
|
|
|
return options.Password;
|
2018-01-29 07:49:43 +08:00
|
|
|
}
|
|
|
|
|
2016-03-05 23:05:47 +08:00
|
|
|
public override ResourceOptions GetDefaultOptions ()
|
|
|
|
{
|
|
|
|
return new ZipOptions {
|
2018-01-09 00:04:23 +08:00
|
|
|
CompressionLevel = Properties.Settings.Default.ZIPCompression,
|
2018-03-17 16:16:53 +08:00
|
|
|
FileNameEncoding = ZipEncoding.Get<Encoding>(),
|
2018-01-30 10:34:24 +08:00
|
|
|
Password = Properties.Settings.Default.ZIPPassword,
|
2016-03-05 23:05:47 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-30 10:34:24 +08:00
|
|
|
public override ResourceOptions GetOptions (object widget)
|
|
|
|
{
|
|
|
|
if (widget is GUI.WidgetZIP)
|
|
|
|
Properties.Settings.Default.ZIPPassword = ((GUI.WidgetZIP)widget).Password.Text;
|
|
|
|
return GetDefaultOptions();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object GetAccessWidget ()
|
|
|
|
{
|
|
|
|
return new GUI.WidgetZIP (DefaultScheme.KnownKeys);
|
|
|
|
}
|
|
|
|
|
2016-03-05 23:05:47 +08:00
|
|
|
// TODO: GUI widget for options
|
|
|
|
|
|
|
|
public override void Create (Stream output, IEnumerable<Entry> list, ResourceOptions options,
|
|
|
|
EntryCallback callback)
|
|
|
|
{
|
2016-03-06 07:28:58 +08:00
|
|
|
var zip_options = GetOptions<ZipOptions> (options);
|
|
|
|
int callback_count = 0;
|
2016-03-05 23:05:47 +08:00
|
|
|
using (var zip = new ZipArchive (output, ZipArchiveMode.Create, true, zip_options.FileNameEncoding))
|
|
|
|
{
|
|
|
|
foreach (var entry in list)
|
|
|
|
{
|
|
|
|
var zip_entry = zip.CreateEntry (entry.Name, zip_options.CompressionLevel);
|
|
|
|
using (var input = File.OpenRead (entry.Name))
|
|
|
|
using (var zip_file = zip_entry.Open())
|
|
|
|
{
|
2016-03-06 07:28:58 +08:00
|
|
|
if (null != callback)
|
|
|
|
callback (++callback_count, entry, arcStrings.MsgAddingFile);
|
2016-03-05 23:05:47 +08:00
|
|
|
input.CopyTo (zip_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-30 10:34:24 +08:00
|
|
|
|
|
|
|
ZipScheme DefaultScheme = new ZipScheme { KnownKeys = new Dictionary<string, string>() };
|
|
|
|
|
|
|
|
public override ResourceScheme Scheme
|
|
|
|
{
|
|
|
|
get { return DefaultScheme; }
|
|
|
|
set { DefaultScheme = (ZipScheme)value; }
|
|
|
|
}
|
2016-03-05 23:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public class ZipOptions : ResourceOptions
|
|
|
|
{
|
|
|
|
public CompressionLevel CompressionLevel { get; set; }
|
|
|
|
public Encoding FileNameEncoding { get; set; }
|
2018-01-30 10:34:24 +08:00
|
|
|
public string Password { get; set; }
|
2016-03-05 21:25:04 +08:00
|
|
|
}
|
|
|
|
}
|