//
// ZIPFile.h
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#pragma once
class CBuffer;
class CZIPFile
{
// Construction
public:
CZIPFile(HANDLE hAttach = INVALID_HANDLE_VALUE);
~CZIPFile();
// File Class
public:
//这个内部类主要是为了解压缩文件而设立的一个指针
class File
{
private:
friend class CZIPFile;
inline File() {};
CZIPFile* m_pZIP;
public:
CBuffer* Decompress();
BOOL Extract(LPCTSTR pszFile);
public:
CString m_sName;
QWORD m_nSize;
protected:
QWORD m_nLocalOffset;
QWORD m_nCompressedSize;
int m_nCompression;
BOOL PrepareToDecompress(LPVOID pStream);
};
// Attributes
protected:
BOOL m_bAttach;
HANDLE m_hFile;
File* m_pFile;
int m_nFile;
// Operations
public:
BOOL Open(LPCTSTR pszFile);
BOOL Attach(HANDLE hFile);
BOOL IsOpen() const;
void Close();
public:
int GetCount() const;
File* GetFile(int nFile) const;
File* GetFile(LPCTSTR pszFile,
BOOL bPartial = FALSE) const;
protected:
BOOL LocateCentralDirectory();
BOOL ParseCentralDirectory(BYTE* pDirectory,
DWORD nDirectory);
BOOL SeekToFile(File* pFile);
};
//
// ZIPFile.cpp
//
// Copyright (c) Shareaza Development Team, 2002-2005.
// This file is part of SHAREAZA (www.shareaza.com)
//
// Shareaza is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// Shareaza 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shareaza; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "StdAfx.h"
#include "ZIPFile.h"
#include "Buffer.h"
#include <zlib.h>
/////////////////////////////////////////////////////////////////////////////
// CZIPFile construction
CZIPFile::CZIPFile(HANDLE hAttach)
{
m_bAttach = FALSE;
m_hFile = INVALID_HANDLE_VALUE;
m_pFile = NULL;
m_nFile = 0;
if ( hAttach != INVALID_HANDLE_VALUE )
{
Attach( hAttach );
}
}
CZIPFile::~CZIPFile()
{
Close();
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile open
//打开参数pszFile指定的文件
BOOL CZIPFile::Open(LPCTSTR pszFile)
{
ASSERT( pszFile != NULL );
//首先关闭原来的handle
Close();
m_bAttach = FALSE;
//打开已经存在的pszFile这个文件
m_hFile = CreateFile( pszFile, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if ( m_hFile == INVALID_HANDLE_VALUE ) //打开失败了
{
return FALSE;
}
if ( LocateCentralDirectory() )
{
return TRUE;
}
else
{
Close();
return FALSE;
}
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile attach
BOOL CZIPFile::Attach( HANDLE hFile )
{
ASSERT( hFile != INVALID_HANDLE_VALUE );
Close();
m_bAttach = TRUE;
m_hFile = hFile;
if ( LocateCentralDirectory() )
{
return TRUE;
}
else
{
Close();
return FALSE;
}
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile open test
BOOL CZIPFile::IsOpen() const
{
return m_hFile != INVALID_HANDLE_VALUE;
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile close
//关闭打开文件句柄m_hFile,释放CZipFile数组m_pFile
void CZIPFile::Close()
{
if ( m_hFile != INVALID_HANDLE_VALUE )
{
if ( ! m_bAttach )
{
CloseHandle( m_hFile );
}
m_hFile = INVALID_HANDLE_VALUE;
}
if ( m_pFile != NULL )
{
delete [] m_pFile;
}
m_pFile = NULL;
m_nFile = 0;
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile get the file count
//返回m_pFile数组中文件的数目
int CZIPFile::GetCount() const
{
return m_nFile;
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile get a particular file
//返回m_pFile数组中第nFile个文件的指针
CZIPFile::File* CZIPFile::GetFile(int nFile) const
{
return ( nFile < 0 || nFile >= m_nFile ) ? NULL : m_pFile + nFile;
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile lookup a file by name
//在m_pFile数组中寻找文件名为参数字符串pszFile的那个File*
CZIPFile::File* CZIPFile::GetFile(LPCTSTR pszFile, BOOL bPartial) const
{
File* pFile = m_pFile;
for ( int nFile = m_nFile ; nFile ; nFile--, pFile++ )
{
if ( bPartial )// 部分是文件名
{
//找到字符/在m_sName中最后一个出现的位置,也就是说后面
//就是单纯的文件名了
LPCTSTR pszName = _tcsrchr( pFile->m_sName, '/' );
pszName = pszName ? pszName + 1 : (LPCTSTR)pFile->m_sName;
//比较单纯的文件名和当前参数pszFile是否相同
if ( _tcsicoll( pszName, pszFile ) == 0 )
{
return pFile;
}
}
else // 全部比较,如果两个字符串完全相等,则返回m_pFile
{
//_tcsicoll比较两个以/0结束的字符串是否相等
if ( _tcsicoll( pFile->m_sName, pszFile ) == 0 )
{
return pFile;
}
}
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile locate the central directory
#pragma pack(1)
typedef struct
{
DWORD nSignature; // 0x06054b50
WORD nThisDisk;
WORD nDirectoryDisk;
WORD nFilesThisDisk;
WORD nTotalFiles;
DWORD nDirectorySize;
DWORD nDirectoryOffset;
WORD nCommentLen;
} ZIP_DIRECTORY_LOC;
#pragma pack()
/*
解析m_hFile文件,建立和填充m_pFile数组的内容
m_hFile文件的后面部分保存了一个ZIP_DIRECTORY_LOC结构;
前面部分从directoryoffset位置开始的directorysize个大小
的缓冲区用来保存m_nFile个文件对象
*/
BOOL CZIPFile::LocateCentralDirectory()
{
BYTE pBuffer[4096];
DWORD nBuffer = 0;
SetFilePointer( m_hFile, -4096, NULL, FILE_END );
//读取4096个字节到pBuffer中
if ( ! ReadFile( m_hFile, pBuffer, 4096, &nBuffer, NULL ) )
{
return FALSE;
}
//读取的字节数比ZIP_DIRECTORY_LOC还小?返回错误
if ( nBuffer < sizeof(ZIP_DIRECTORY_LOC) )
{
return FALSE;
}
//在pBuffer中寻找dword0x06054b50,从末尾开始向后找
ZIP_DIRECTORY_LOC* pLoc = NULL;
for ( DWORD nScan = 4 ; nScan < nBuffer ; nScan++ )
{
DWORD* pnSignature = (DWORD*)( pBuffer + nBuffer - nScan );
if ( *pnSignature == 0x06054b50 )
{
pLoc = (ZIP_DIRECTORY_LOC*)pnSignature;
break;
}
}
if ( pLoc == NULL ) // 没找到,返回false
{
return FALSE;
}
//确保signature是0x06054b50
ASSERT( pLoc->nSignature == 0x06054b50 );
//文件大小比ploc结构的nDirectorySize还要小,返回false
if ( GetFileSize( m_hFile, NULL ) < pLoc->nDirectorySize )
{
return FALSE;
}
//设置文件指针到pLoc->nDirectoryOffset位置,从文件开头数的
if ( SetFilePointer( m_hFile, pLoc->nDirectoryOffset, NULL, FILE_BEGIN )
!= pLoc->nDirectoryOffset )
{
return FALSE;
}
//读取pLoc->nDirectorySize个字符到pDirectory数组中
BYTE* pDirectory = new BYTE[ pLoc->nDirectorySize ];
ReadFile( m_hFile, pDirectory, pLoc->nDirectorySize, &nBuffer, NULL );
if ( nBuffer == pLoc->nDirectorySize ) // 读取成功了
{
m_nFile = (int)pLoc->nTotalFiles;//m_nFile设置为pLoc的文件总数
m_pFile = new File[ m_nFile ];//创建m_nFile个文件的数组
//调用Parse完成解析
if ( ! ParseCentralDirectory( pDirectory, pLoc->nDirectorySize ) )
{
delete [] m_pFile;
m_pFile = NULL;
m_nFile = 0;
}
}
delete [] pDirectory;
return ( m_nFile > 0 );
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile parse the central directory
#pragma pack(1)
typedef struct
{
DWORD nSignature; // 0x02014b50
WORD nWriteVersion;
WORD nReadVersion;
WORD nFlags;
WORD nCompression;
WORD nFileTime;
WORD nFileDate;
DWORD nCRC;
DWORD nCompressedSize;
DWORD nActualSize;
WORD nNameLen;
WORD nExtraLen;
WORD nCommentLen;
WORD nStartDisk;
WORD nInternalAttr;
DWORD nExternalAttr;
DWORD nLocalOffset;
} ZIP_CENTRAL_FILE;
#pragma pack()
/*
pDirectory数组的结构为m_nFile个单位,
每个单位的构成为一个ZIP_CENTRAL_FILE结构后面跟着namelen个字符的名字,
nExtraLen和nCommentLen个字符的空间;根据pDirectory数组的内容设置
m_pFile数组中各个项的各个属性
*/
BOOL CZIPFile::ParseCentralDirectory(BYTE* pDirectory, DWORD nDirectory)
{
for ( int nFile = 0 ; nFile < m_nFile ; nFile++ )
{
//首先是ZIP_CENTRAL_FILE结构
ZIP_CENTRAL_FILE* pRecord = (ZIP_CENTRAL_FILE*)pDirectory;
if ( nDirectory < sizeof(*pRecord) )
{
return FALSE;
}
//signature必须是0x02014b50
if ( pRecord->nSignature != 0x02014b50 )
{
return FALSE;
}
//指向Record结构后面的第一个字节
pDirectory += sizeof(*pRecord);
nDirectory -= sizeof(*pRecord);
//nNameLen,nExtraLen和nCommentLen构成了nTailLen
int nTailLen = (int)pRecord->nNameLen + (int)pRecord->nExtraLen
+ (int)pRecord->nCommentLen;
if ( nDirectory < (DWORD)nTailLen )
{
return FALSE;
}
//根据pRecord的各项属性来设置m_pFile数组第nFile项的内容
m_pFile[ nFile ].m_pZIP = this;
m_pFile[ nFile ].m_nSize = pRecord->nActualSize;
m_pFile[ nFile ].m_nLocalOffset = pRecord->nLocalOffset;
m_pFile[ nFile ].m_nCompressedSize = pRecord->nCompressedSize;
m_pFile[ nFile ].m_nCompression = pRecord->nCompression;
//record结构后面是name,将name属性设置为后面的namelen个字符
//将/字符替换为/字符
LPTSTR pszName = m_pFile[ nFile ].m_sName.GetBuffer( pRecord->nNameLen );
for ( WORD nChar = 0 ; nChar < pRecord->nNameLen ; nChar++ )
{
pszName[ nChar ] = (TCHAR)pDirectory[ nChar ];
if ( pszName[ nChar ] == '//' )
{
pszName[ nChar ] = '/';
}
}
m_pFile[ nFile ].m_sName.ReleaseBuffer( pRecord->nNameLen );
pDirectory += (DWORD)nTailLen;
nDirectory -= (DWORD)nTailLen;
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile::File seek to a file
#pragma pack(1)
typedef struct
{
DWORD nSignature; // 0x04034b50
WORD nVersion;
WORD nFlags;
WORD nCompression;
WORD nFileTime;
WORD nFileDate;
DWORD nCRC;
DWORD nCompressedSize;
DWORD nActualSize;
WORD nNameLen;
WORD nExtraLen;
} ZIP_LOCAL_FILE;
#pragma pack()
//将指针搬到pFile->m_nLocalOffset位置
//读取一个ZIP_LOCAL_FILE结构,然后将文件指针搬到结构的最后
/*
File对象pFile的m_pZip指向当前的CZipFile对象,这个File对象
只是指明了一些偏移量等数据,根据这些偏移量在m_hFile中
读取ZIP_LOCAL_FILE结构
*/
BOOL CZIPFile::SeekToFile( File* pFile )
{
ASSERT( this != NULL );
ASSERT( pFile != NULL );
ASSERT( pFile->m_pZIP == this ); //
if ( m_hFile == INVALID_HANDLE_VALUE )
{
return FALSE;
}
//将文件指针搬到从文件开头数pFile->m_nLocalOffset个字节的位置
if ( SetFilePointer( m_hFile, (DWORD)pFile->m_nLocalOffset,
NULL, FILE_BEGIN ) != pFile->m_nLocalOffset )
{
return FALSE;
}
ZIP_LOCAL_FILE pLocal;
DWORD nRead = 0;
//从m_hFile文件中读取一个ZIP_LOCAL_FILE结构到pLocal中
ReadFile( m_hFile, &pLocal, sizeof(pLocal), &nRead, NULL );
if ( nRead != sizeof(pLocal) )
{
return FALSE;
}
//签名一定得是0x04034b50
if ( pLocal.nSignature != 0x04034b50 )
{
return FALSE;
}
//pLocal.nCompression只能是Z_DEFLATED和0两种情况
if ( pLocal.nCompression != Z_DEFLATED
&& pLocal.nCompression != 0 )
{
return FALSE;
}
//将m_hFile的文件指针搬到commentlen开始的位置
SetFilePointer( m_hFile, pLocal.nNameLen + pLocal.nExtraLen,
NULL, FILE_CURRENT );
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile::File prepare to decompress
//如果没有压缩,什么也不做,否则初始化压缩流
BOOL CZIPFile::File::PrepareToDecompress(LPVOID pStream)
{
ZeroMemory( pStream, sizeof(z_stream) );
if ( ! m_pZIP->SeekToFile( this ) )
{
return FALSE;
}
if ( m_nCompression == 0 )
{
return ( m_nSize == m_nCompressedSize );
}
else
{
ASSERT( m_nCompression == Z_DEFLATED );
return Z_OK == inflateInit2( (z_stream*)pStream, -MAX_WBITS );
}
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile::File decompress to memory
//解压缩
/*
从m_pZIP->m_hFile文件中读取需要解压缩的数据,解压缩到一个CBuffer
中,返回结果CBuffer;首先调用PrepareToDecompress将
*/
CBuffer* CZIPFile::File::Decompress()
{
z_stream pStream;
//文件大小不能超过32M
if ( m_nSize > 32*1024*1024 )
{
return NULL;
}
//准备解压缩,初始化解压缩stream
if ( ! PrepareToDecompress( &pStream ) )
{
return NULL;
}
if ( m_nCompression == 0 ) // 没有压缩
{
//直接读文件到一个CBuffer中,返回这个CBuffer
CBuffer* pTarget = new CBuffer();
pTarget->EnsureBuffer( (DWORD)m_nSize );
ReadFile( m_pZIP->m_hFile, pTarget->m_pBuffer,
(DWORD)m_nSize, &pTarget->m_nLength, NULL );
if ( pTarget->m_nLength == (DWORD)m_nSize )
{
return pTarget;
}
delete pTarget;
return NULL;
}
//进行了压缩,调用inflate完成解压缩过程
DWORD nSource = (DWORD)m_nCompressedSize;
//pSource保存了原始的压缩文件
BYTE* pSource = new BYTE[ nSource ];
ReadFile( m_pZIP->m_hFile, pSource, nSource, &nSource, NULL );
if ( nSource != (DWORD)m_nCompressedSize )
{
inflateEnd( &pStream );
return NULL;
}
//创建一个CBuffer用来保存解压缩后的结果
CBuffer* pTarget = new CBuffer();
pTarget->EnsureBuffer( (DWORD)m_nSize );
pTarget->m_nLength = (DWORD)m_nSize;
pStream.next_in = pSource;
pStream.avail_in = (DWORD)m_nCompressedSize;
pStream.next_out = pTarget->m_pBuffer;
pStream.avail_out = pTarget->m_nLength;
//启动解压缩过程
inflate( &pStream, Z_FINISH );
//释放资源
delete [] pSource;
if ( pStream.avail_out != 0 )
{
delete pTarget;
pTarget = NULL;
}
inflateEnd( &pStream );
//返回保存压缩结果的CBuffer
return pTarget;
}
/////////////////////////////////////////////////////////////////////////////
// CZIPFile::File decompress to disk
#define BUFFER_IN_SIZE (64*1024)
#define BUFFER_OUT_SIZE (128*1024)
//创建参数指定的文件,将m_pZIP->m_hFile文件解压缩后的结果写入到这个文件中
BOOL CZIPFile::File::Extract(LPCTSTR pszFile)
{
z_stream pStream;
/*
打开pszFile指定的文件。准备开始解压缩,hFile是
要写入数据的目的文件,需要创建之
*/
HANDLE hFile;
hFile = CreateFile( pszFile, GENERIC_WRITE, 0, NULL, CREATE_NEW,
FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile == INVALID_HANDLE_VALUE )
{
return FALSE;
}
if ( ! PrepareToDecompress( &pStream ) )
{
return NULL;
}
//nCompressed保存了当前解压缩的源文件的数目,nUncompressed保存了
//当前解压缩后变成了多少个字节
QWORD nCompressed = 0, nUncompressed = 0;
if ( m_nCompression == Z_DEFLATED ) // 压缩了
{
BYTE* pBufferIn = new BYTE[BUFFER_IN_SIZE];
BYTE* pBufferOut = new BYTE[BUFFER_OUT_SIZE];
while ( nCompressed < m_nCompressedSize
|| nUncompressed < m_nSize )
{
if ( pStream.avail_in == 0 )
{
pStream.avail_in = (DWORD)min(
m_nCompressedSize - nCompressed,
QWORD(BUFFER_IN_SIZE) );
pStream.next_in = pBufferIn;
DWORD nRead = 0;
//从m_pZIP->m_hFile文件中读取数据到pBufferIn中
ReadFile( m_pZIP->m_hFile, pBufferIn,
pStream.avail_in, &nRead, NULL );
if ( nRead != pStream.avail_in )
{
break;
}
nCompressed += nRead;
}
pStream.avail_out = BUFFER_OUT_SIZE;
pStream.next_out = pBufferOut;
//解压缩到pBufferOut中
int nInflate = inflate( &pStream, Z_SYNC_FLUSH );
if ( pStream.avail_out < BUFFER_OUT_SIZE )
{
//将解压缩后的数组内容写入到文件hFile中
DWORD nWrite = BUFFER_OUT_SIZE - pStream.avail_out;
WriteFile( hFile, pBufferOut, nWrite, &nWrite, NULL );
if ( nWrite != BUFFER_OUT_SIZE - pStream.avail_out )
{
break;
}
nUncompressed += nWrite;
}
} // end of while
delete [] pBufferOut;
delete [] pBufferIn;
inflateEnd( &pStream );
}
else // 没有压缩,则从文件m_pZIP->m_hFile中读取出数据
{
// 然后写入到hFile中
BYTE* pBufferOut = new BYTE[BUFFER_OUT_SIZE];
while ( nUncompressed < m_nSize )
{
DWORD nChunk = (DWORD)min( m_nSize - nUncompressed,
QWORD(BUFFER_OUT_SIZE) );
DWORD nProcess = 0;
ReadFile( m_pZIP->m_hFile, pBufferOut,
nChunk, &nProcess, NULL );
if ( nChunk != nProcess )
{
break;
}
WriteFile( hFile, pBufferOut, nChunk, &nProcess, NULL );
if ( nChunk != nProcess )
{
break;
}
nCompressed += nChunk;
nUncompressed += nChunk;
}
delete [] pBufferOut;
}
CloseHandle( hFile );
//解压缩后的字节比原来的大小大,则成功了,返回之
if ( nUncompressed >= m_nSize )
{
return TRUE;
}
DeleteFile( pszFile );
return FALSE;
}