📄 ziphandler.cpp
字号:
// ZipHandler.cpp
#include "StdAfx.h"
#include "ZipHandler.h"
#include "Common/Defs.h"
#include "Common/StringConvert.h"
#include "Common/ComTry.h"
#include "Common/IntToString.h"
#include "Windows/Time.h"
#include "Windows/PropVariant.h"
#include "../../IPassword.h"
#include "../../Common/ProgressUtils.h"
#include "../../Common/StreamObjects.h"
#include "../../Common/CreateCoder.h"
#include "../../Common/FilterCoder.h"
#include "../../Compress/Copy/CopyCoder.h"
#include "../Common/ItemNameUtils.h"
#include "../Common/OutStreamWithCRC.h"
#include "../../Compress/Shrink/ShrinkDecoder.h"
#include "../../Compress/Implode/ImplodeDecoder.h"
#include "../../Crypto/Zip/ZipCipher.h"
#include "../../Crypto/WzAES/WzAES.h"
#ifdef ZIP_STRONG_SUPORT
#include "../../Crypto/ZipStrong/ZipStrong.h"
#endif
using namespace NWindows;
namespace NArchive {
namespace NZip {
// static const CMethodId kMethodId_Store = 0;
static const CMethodId kMethodId_ZipBase = 0x040100;
static const CMethodId kMethodId_BZip2 = 0x040202;
const wchar_t *kHostOS[] =
{
L"FAT",
L"AMIGA",
L"VMS",
L"Unix",
L"VM/CMS",
L"Atari",
L"HPFS",
L"Macintosh",
L"Z-System",
L"CP/M",
L"TOPS-20",
L"NTFS",
L"SMS/QDOS",
L"Acorn",
L"VFAT",
L"MVS",
L"BeOS",
L"Tandem",
L"OS/400",
L"OS/X"
};
static const int kNumHostOSes = sizeof(kHostOS) / sizeof(kHostOS[0]);
static const wchar_t *kUnknownOS = L"Unknown";
STATPROPSTG kProps[] =
{
{ NULL, kpidPath, VT_BSTR},
{ NULL, kpidIsDir, VT_BOOL},
{ NULL, kpidSize, VT_UI8},
{ NULL, kpidPackSize, VT_UI8},
{ NULL, kpidMTime, VT_FILETIME},
{ NULL, kpidCTime, VT_FILETIME},
{ NULL, kpidATime, VT_FILETIME},
{ NULL, kpidAttrib, VT_UI4},
{ NULL, kpidEncrypted, VT_BOOL},
{ NULL, kpidComment, VT_BSTR},
{ NULL, kpidCRC, VT_UI4},
{ NULL, kpidMethod, VT_BSTR},
{ NULL, kpidHostOS, VT_BSTR}
// { NULL, kpidUnpackVer, VT_UI1},
};
const wchar_t *kMethods[] =
{
L"Store",
L"Shrink",
L"Reduced1",
L"Reduced2",
L"Reduced2",
L"Reduced3",
L"Implode",
L"Tokenizing",
L"Deflate",
L"Deflate64",
L"PKImploding",
L"Unknown",
L"BZip2"
};
const int kNumMethods = sizeof(kMethods) / sizeof(kMethods[0]);
// const wchar_t *kUnknownMethod = L"Unknown";
const wchar_t *kWavPackMethod = L"WavPack";
const wchar_t *kPPMdMethod = L"PPMd";
const wchar_t *kAESMethod = L"AES";
const wchar_t *kZipCryptoMethod = L"ZipCrypto";
const wchar_t *kStrongCryptoMethod = L"StrongCrypto";
struct CStrongCryptoPair
{
UInt16 Id;
const wchar_t *Name;
};
CStrongCryptoPair g_StrongCryptoPairs[] =
{
{ NStrongCryptoFlags::kDES, L"DES" },
{ NStrongCryptoFlags::kRC2old, L"RC2a" },
{ NStrongCryptoFlags::k3DES168, L"3DES-168" },
{ NStrongCryptoFlags::k3DES112, L"3DES-112" },
{ NStrongCryptoFlags::kAES128, L"pkAES-128" },
{ NStrongCryptoFlags::kAES192, L"pkAES-192" },
{ NStrongCryptoFlags::kAES256, L"pkAES-256" },
{ NStrongCryptoFlags::kRC2, L"RC2" },
{ NStrongCryptoFlags::kBlowfish, L"Blowfish" },
{ NStrongCryptoFlags::kTwofish, L"Twofish" },
{ NStrongCryptoFlags::kRC4, L"RC4" }
};
STATPROPSTG kArcProps[] =
{
{ NULL, kpidBit64, VT_BOOL},
{ NULL, kpidComment, VT_BSTR}
};
CHandler::CHandler()
{
InitMethodProperties();
}
static AString BytesToString(const CByteBuffer &data)
{
AString s;
int size = (int)data.GetCapacity();
if (size > 0)
{
char *p = s.GetBuffer(size + 1);
memcpy(p, (const Byte *)data, size);
p[size] = '\0';
s.ReleaseBuffer();
}
return s;
}
IMP_IInArchive_Props
IMP_IInArchive_ArcProps
STDMETHODIMP CHandler::GetArchiveProperty(PROPID propID, PROPVARIANT *value)
{
COM_TRY_BEGIN
NWindows::NCOM::CPropVariant prop;
switch(propID)
{
case kpidBit64: if (m_Archive.IsZip64) prop = m_Archive.IsZip64; break;
case kpidComment:
prop = MultiByteToUnicodeString(BytesToString(m_Archive.m_ArchiveInfo.Comment), CP_ACP);
break;
}
prop.Detach(value);
COM_TRY_END
return S_OK;
}
STDMETHODIMP CHandler::GetNumberOfItems(UInt32 *numItems)
{
*numItems = m_Items.Size();
return S_OK;
}
STDMETHODIMP CHandler::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
{
COM_TRY_BEGIN
NWindows::NCOM::CPropVariant prop;
const CItemEx &item = m_Items[index];
switch(propID)
{
case kpidPath: prop = NItemName::GetOSName2(item.GetUnicodeString(item.Name)); break;
case kpidIsDir: prop = item.IsDir(); break;
case kpidSize: prop = item.UnPackSize; break;
case kpidPackSize: prop = item.PackSize; break;
case kpidTimeType:
{
FILETIME utcFileTime;
if (item.CentralExtra.GetNtfsTime(NFileHeader::NNtfsExtra::kTagTime, utcFileTime))
prop = (UInt32)NFileTimeType::kWindows;
break;
}
case kpidCTime:
{
FILETIME ft;
if (item.CentralExtra.GetNtfsTime(NFileHeader::NNtfsExtra::kCTime, ft))
prop = ft;
break;
}
case kpidATime:
{
FILETIME ft;
if (item.CentralExtra.GetNtfsTime(NFileHeader::NNtfsExtra::kATime, ft))
prop = ft;
break;
}
case kpidMTime:
{
FILETIME utcFileTime;
if (!item.CentralExtra.GetNtfsTime(NFileHeader::NNtfsExtra::kMTime, utcFileTime))
{
FILETIME localFileTime;
if (NTime::DosTimeToFileTime(item.Time, localFileTime))
{
if (!LocalFileTimeToFileTime(&localFileTime, &utcFileTime))
utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
}
else
utcFileTime.dwHighDateTime = utcFileTime.dwLowDateTime = 0;
}
prop = utcFileTime;
break;
}
case kpidAttrib: prop = item.GetWinAttributes(); break;
case kpidEncrypted: prop = item.IsEncrypted(); break;
case kpidComment: prop = item.GetUnicodeString(BytesToString(item.Comment)); break;
case kpidCRC: if (item.IsThereCrc()) prop = item.FileCRC; break;
case kpidMethod:
{
UInt16 methodId = item.CompressionMethod;
UString method;
if (item.IsEncrypted())
{
if (methodId == NFileHeader::NCompressionMethod::kWzAES)
{
method = kAESMethod;
CWzAesExtraField aesField;
if (item.CentralExtra.GetWzAesField(aesField))
{
method += L"-";
wchar_t s[32];
ConvertUInt64ToString((aesField.Strength + 1) * 64 , s);
method += s;
method += L" ";
methodId = aesField.Method;
}
}
else
{
if (item.IsStrongEncrypted())
{
CStrongCryptoField f;
bool finded = false;
if (item.CentralExtra.GetStrongCryptoField(f))
{
for (int i = 0; i < sizeof(g_StrongCryptoPairs) / sizeof(g_StrongCryptoPairs[0]); i++)
{
const CStrongCryptoPair &pair = g_StrongCryptoPairs[i];
if (f.AlgId == pair.Id)
{
method += pair.Name;
finded = true;
break;
}
}
}
if (!finded)
method += kStrongCryptoMethod;
}
else
method += kZipCryptoMethod;
method += L" ";
}
}
if (methodId < kNumMethods)
method += kMethods[methodId];
else if (methodId == NFileHeader::NCompressionMethod::kPPMd)
method += kPPMdMethod;
else if (methodId == NFileHeader::NCompressionMethod::kWavPack)
method += kWavPackMethod;
else
{
wchar_t s[32];
ConvertUInt64ToString(methodId, s);
method += s;
}
prop = method;
break;
}
case kpidHostOS:
prop = (item.MadeByVersion.HostOS < kNumHostOSes) ?
(kHostOS[item.MadeByVersion.HostOS]) : kUnknownOS;
break;
}
prop.Detach(value);
return S_OK;
COM_TRY_END
}
class CProgressImp: public CProgressVirt
{
CMyComPtr<IArchiveOpenCallback> _callback;
public:
STDMETHOD(SetTotal)(UInt64 numFiles);
STDMETHOD(SetCompleted)(UInt64 numFiles);
CProgressImp(IArchiveOpenCallback *callback): _callback(callback) {}
};
STDMETHODIMP CProgressImp::SetTotal(UInt64 numFiles)
{
if (_callback)
return _callback->SetTotal(&numFiles, NULL);
return S_OK;
}
STDMETHODIMP CProgressImp::SetCompleted(UInt64 numFiles)
{
if (_callback)
return _callback->SetCompleted(&numFiles, NULL);
return S_OK;
}
STDMETHODIMP CHandler::Open(IInStream *inStream,
const UInt64 *maxCheckStartPosition, IArchiveOpenCallback *callback)
{
COM_TRY_BEGIN
try
{
Close();
RINOK(inStream->Seek(0, STREAM_SEEK_SET, NULL));
RINOK(m_Archive.Open(inStream, maxCheckStartPosition));
CProgressImp progressImp(callback);
return m_Archive.ReadHeaders(m_Items, &progressImp);
}
catch(const CInArchiveException &) { Close(); return S_FALSE; }
catch(...) { Close(); throw; }
COM_TRY_END
}
STDMETHODIMP CHandler::Close()
{
m_Items.Clear();
m_Archive.Close();
return S_OK;
}
//////////////////////////////////////
// CHandler::DecompressItems
struct CMethodItem
{
UInt16 ZipMethod;
CMyComPtr<ICompressCoder> Coder;
};
class CZipDecoder
{
NCrypto::NZip::CDecoder *_zipCryptoDecoderSpec;
NCrypto::NWzAES::CDecoder *_aesDecoderSpec;
CMyComPtr<ICompressFilter> _zipCryptoDecoder;
CMyComPtr<ICompressFilter> _aesDecoder;
#ifdef ZIP_STRONG_SUPORT
NCrypto::NZipStrong::CDecoder *_zsDecoderSpec;
CMyComPtr<ICompressFilter> _zsDecoder;
#endif
CFilterCoder *filterStreamSpec;
CMyComPtr<ISequentialInStream> filterStream;
CMyComPtr<ICryptoGetTextPassword> getTextPassword;
CObjectVector<CMethodItem> methodItems;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -