mirror of
https://github.com/crskycode/GARbro.git
synced 2024-12-28 13:54:15 +08:00
implemented ZIP archives.
just a wrapper around .Net library class.
This commit is contained in:
parent
1749b93d7a
commit
14959d1d36
147
ArcFormats/ArcZIP.cs
Normal file
147
ArcFormats/ArcZIP.cs
Normal file
@ -0,0 +1,147 @@
|
||||
//! \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;
|
||||
|
||||
namespace GameRes.Formats.PkWare
|
||||
{
|
||||
internal class ZipEntry : PackedEntry
|
||||
{
|
||||
public readonly ZipArchiveEntry NativeEntry;
|
||||
|
||||
public ZipEntry (ZipArchiveEntry zip_entry)
|
||||
{
|
||||
NativeEntry = zip_entry;
|
||||
Name = zip_entry.FullName;
|
||||
Type = FormatCatalog.Instance.GetTypeFromName (zip_entry.FullName);
|
||||
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.
|
||||
Size = (uint)Math.Min (zip_entry.CompressedLength, uint.MaxValue);
|
||||
UnpackedSize = (uint)Math.Min (zip_entry.Length, uint.MaxValue);
|
||||
// offset is unknown and reported as '0' for all files.
|
||||
Offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal class PkZipArchive : ArcFile
|
||||
{
|
||||
readonly ZipArchive m_zip;
|
||||
|
||||
public PkZipArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, ZipArchive native)
|
||||
: base (arc, impl, dir)
|
||||
{
|
||||
m_zip = native;
|
||||
}
|
||||
|
||||
#region IDisposable implementation
|
||||
bool _zip_disposed = false;
|
||||
protected override void Dispose (bool disposing)
|
||||
{
|
||||
if (_zip_disposed)
|
||||
return;
|
||||
|
||||
if (disposing)
|
||||
m_zip.Dispose();
|
||||
_zip_disposed = true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
[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; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
if (-1 == SearchForSignature (file))
|
||||
return null;
|
||||
var input = file.CreateStream();
|
||||
try
|
||||
{
|
||||
var zip = new ZipArchive (input, ZipArchiveMode.Read, false, Encodings.cp932);
|
||||
try
|
||||
{
|
||||
var dir = zip.Entries.Select (z => new ZipEntry (z) as Entry).ToList();
|
||||
return new PkZipArchive (file, this, dir, zip);
|
||||
}
|
||||
catch
|
||||
{
|
||||
zip.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
input.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
||||
{
|
||||
return ((ZipEntry)entry).NativeEntry.Open();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
private unsafe long SearchForSignature (ArcView file)
|
||||
{
|
||||
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))
|
||||
{
|
||||
byte* ptr_end = view.GetPointer (start_offset);
|
||||
byte* ptr = ptr_end + tail_size-0x16;
|
||||
try {
|
||||
for (; ptr >= ptr_end; --ptr)
|
||||
{
|
||||
if (6 == ptr[3] && 5 == ptr[2] && 'K' == ptr[1] && 'P' == ptr[0])
|
||||
return start_offset + (ptr-ptr_end);
|
||||
}
|
||||
return -1;
|
||||
} finally {
|
||||
view.SafeMemoryMappedViewHandle.ReleasePointer();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user