最近在做一个项目,其中会用到10000多个小图片,拷贝起来特别慢。所以做了这个程序,将所有小文件写到一起,并建立了一个索引文件,存放文件名,偏移量,长度。这样可以很快速的拷贝到SD卡上,当需要其中的图片时,就将它从大文件中读出来。程序非常之简单。。。因为时间比较仓促,所有还没有优化(压缩,建立效率更高的索引)
PicAggr.cpp
BOOL CPicAggrDlg::OnInitDialog()
{
CDialog::OnInitDialog();
......
// TODO: Add extra initialization here
memset( m_szSrcPath, 0, sizeof(m_szSrcPath) );
memset( m_szDestPath, 0, sizeof(m_szDestPath) );
memset( &m_FileRec, 0, sizeof(FILEREC) );
return TRUE; // return TRUE unless you set the focus to a control
}
void CPicAggrDlg::OnButtonSrcBrowse()
{
// TODO: Add your control notification handler code here
BROWSEINFO browseinfo;
LPITEMIDLIST lpItemList;
TCHAR szPath[MAX_PATH] = {0};
memset( &browseinfo, 0, sizeof(BROWSEINFO) );
browseinfo.hwndOwner = this->GetSafeHwnd();
browseinfo.pszDisplayName = szPath;
browseinfo.lpszTitle = "请选择文件所在的目录";
if ( lpItemList = SHBrowseForFolder( &browseinfo ) )
{
if ( SHGetPathFromIDList( lpItemList, m_szSrcPath ) )
{
SetDlgItemText( IDC_EDITSRCPATH, m_szSrcPath );
}
else
{
MessageBox( "ERROR:Get Path", NULL, MB_OK );
}
}
else
{
MessageBox( "ERROR:Browse Folder", NULL, MB_OK );
}
}
void CPicAggrDlg::OnButtonDestBrowse()
{
// TODO: Add your control notification handler code here
BROWSEINFO browseinfo;
LPITEMIDLIST lpItemList;
TCHAR szPath[MAX_PATH] = {0};
memset( &browseinfo, 0, sizeof(BROWSEINFO) );
browseinfo.hwndOwner = this->GetSafeHwnd();
browseinfo.pszDisplayName = szPath;
browseinfo.lpszTitle = "请选择文件所在的目录";
if ( lpItemList = SHBrowseForFolder( &browseinfo ) )
{
if ( SHGetPathFromIDList( lpItemList, m_szDestPath ) )
{
SetDlgItemText( IDC_EDITDESTPATH, m_szDestPath );
}
else
{
MessageBox( "ERROR:Get Path", NULL, MB_OK );
}
}
else
{
MessageBox( "ERROR:Browse Folder", NULL, MB_OK );
}
}
void CPicAggrDlg::OnButtonAggr()
{
// TODO: Add your control notification handler code here
WIN32_FIND_DATA findfile;
memset( &findfile, 0, sizeof(WIN32_FIND_DATA) );
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR szPicPath[MAX_PATH];
unsigned int nDestLen = 0;
FILE * fpIndex = NULL;
FILE * fpSrc = NULL;
FILE * fpDest = NULL;
int nRead = 0;
TCHAR szBuf[1024] = {0};
TCHAR szIndexPath[MAX_PATH] = {0};
if ( _tcslen(m_szSrcPath) == 0 )
{
_tcscpy( m_szSrcPath, "." );
}
_tcscat( m_szSrcPath, "//" );
_tcscpy( szPicPath, m_szSrcPath );
_tcscat( szPicPath, "*.*" );
if ( _tcslen(m_szDestPath) == 0 )
{
_tcscpy( m_szDestPath, "." );
}
_tcscat( m_szDestPath, "//" );
_tcscat( m_szDestPath, "LTBFTC" );
_tcscpy( szIndexPath, m_szDestPath );
_tcscat( szIndexPath, ".txt" );
fpIndex = _tfopen( szIndexPath, "a" );
fpDest = _tfopen( m_szDestPath, "wb" );
if ( fpDest == NULL )
{
MessageBox( "ERROR:Open Dest File", NULL, MB_OK );
}
hFind = FindFirstFile( szPicPath, &findfile );
if ( hFind != INVALID_HANDLE_VALUE )
{
if ( findfile.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
{
memset( &m_FileRec, 0, sizeof(FILEREC) );
memset( szPicPath, 0, sizeof(szPicPath) );
_tcscpy( szPicPath, m_szSrcPath );
_tcscat( szPicPath, findfile.cFileName );
fpSrc = _tfopen( szPicPath, "rb" );
if ( fpSrc == NULL )
{
MessageBox( "ERROR:Open Dest File", NULL, MB_OK );
}
_tcscpy( m_FileRec.szFileName, szPicPath );
while ( nRead = fread( szBuf, sizeof(TCHAR), sizeof(szBuf), fpSrc ))
{
fwrite( szBuf, sizeof(TCHAR), nRead, fpDest );
memset( szBuf, 0, sizeof(szBuf) );
m_FileRec.nFileLen += nRead;
}
*(_tcsstr(_tcsrev(m_FileRec.szFileName), "//")) = '/0';
_tcsrev( m_FileRec.szFileName );
nDestLen += m_FileRec.nFileLen;
m_FileRec.nFilePos = nDestLen - m_FileRec.nFileLen;
_ftprintf( fpIndex, "%s,%u,%u/n", m_FileRec.szFileName, m_FileRec.nFilePos, m_FileRec.nFileLen );
nRead = 0;
fflush( fpIndex );
fflush( fpDest );
fclose( fpSrc );
}
}
else
{
MessageBox( "ERROR:Find File", NULL, MB_OK );
}
while ( FindNextFile( hFind, &findfile ) != 0 )
{
if ( findfile.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
{
memset( &m_FileRec, 0, sizeof(FILEREC) );
memset( szPicPath, 0, sizeof(szPicPath) );
_tcscpy( szPicPath, m_szSrcPath );
_tcscat( szPicPath, findfile.cFileName );
fpSrc = _tfopen( szPicPath, "rb" );
if ( fpSrc == NULL )
{
MessageBox( "ERROR:Open Dest File", NULL, MB_OK );
}
_tcscpy( m_FileRec.szFileName, szPicPath );
while ( nRead = fread( szBuf, sizeof(TCHAR), sizeof(szBuf), fpSrc ))
{
fwrite( szBuf, sizeof(TCHAR), nRead, fpDest );
memset( szBuf, 0, sizeof(szBuf) );
m_FileRec.nFileLen += nRead;
}
*(_tcsstr(_tcsrev(m_FileRec.szFileName), "//")) = '/0';
_tcsrev( m_FileRec.szFileName );
nDestLen += m_FileRec.nFileLen;
m_FileRec.nFilePos = nDestLen - m_FileRec.nFileLen;
_ftprintf( fpIndex, "%s,%u,%u/n", m_FileRec.szFileName, m_FileRec.nFilePos, m_FileRec.nFileLen );
nRead = 0;
fflush( fpIndex );
fflush( fpDest );
fclose( fpSrc );
}
}
FindClose( hFind );
fclose( fpDest );
fclose( fpIndex );
CDialog::OnOK();
}
void CPicAggrDlg::OnButtonreturn()
{
// TODO: Add your control notification handler code here
TCHAR szReturn[MAX_FNAME_LEN];
FILE * fpSrc = NULL;
FILE * fpIndex = NULL;
FILE * fpReturn = NULL;
TCHAR szBuf[1024] = {0};
TCHAR szIndex[256];
TCHAR * pPos = NULL;
TCHAR * pLen = NULL;
unsigned int nPos = 0;
unsigned int nLen = 0;
unsigned int nReserve = 1024;
bool bFind = false;
GetDlgItemText( IDC_EDITRETURN, szReturn, sizeof(szReturn) );
if ( _tcslen(szReturn) == 0 )
{
MessageBox( "ERROR:Return File", NULL, MB_OK );
}
_tcscat( m_szDestPath, "//LTBFTC");
fpSrc = _tfopen( m_szDestPath, "rb" );
if ( fpSrc == NULL )
{
MessageBox( "ERROR:Open LTBFTC", NULL, MB_OK );
}
_tcscat( m_szDestPath, ".txt" );
fpIndex = _tfopen( m_szDestPath, "r" );
if ( fpIndex == NULL )
{
MessageBox( "ERROR:Open Index", NULL, MB_OK );
}
TCHAR szTest[256] = {0};
_tcscpy( szTest, _tcscat(_tcsrev(_tcsstr(_tcsrev(m_szDestPath), "//")), szReturn) );
fpReturn = _tfopen( szTest, "wb" );
if ( fpReturn == NULL )
{
MessageBox( "ERROR:Return File", NULL, MB_OK );
}
while ( fgets( szIndex, sizeof(szIndex), fpIndex) )
{
if ( _tcsstr( szIndex, szReturn ) != NULL )
{
bFind = true;
break;
}
}
if ( bFind == true )
{
pPos = _tcsstr( szIndex, "," );
pPos++;
pLen = _tcsstr( pPos, "," );
TCHAR szInt[256] = {0};
_tcsncpy( szInt, pPos, pLen-pPos );
pLen++;
nPos = _ttoi( szInt );
_tcsncpy( szInt, pLen, _tcslen(pLen) );
nLen = _ttoi( szInt );
fseek( fpSrc, nPos, SEEK_SET );
while ( fread( szBuf, sizeof(TCHAR), nReserve, fpSrc ) )
{
fwrite( szBuf, sizeof(TCHAR), sizeof(szBuf), fpReturn );
memset( szBuf, 0, sizeof(szBuf) );
nLen -= nReserve;
if ( nLen < 1024 )
{
nReserve = nLen;
}
}
}
else
{
MessageBox( "ERROR:Not Found", NULL, MB_OK );
}
CDialog::OnOK();
}
*******************************************************************************************************************
PicAggrDlg.h
#define MAX_FNAME_LEN 256 // Max size of field names
typedef struct _FILENAME_LENGTH
{
TCHAR szFileName[MAX_FNAME_LEN];
unsigned int nFilePos;
unsigned int nFileLen;
}FILEREC, * PFILEREC;
/
// CPicAggrDlg dialog
class CPicAggrDlg : public CDialog
{
// Construction
public:
CPicAggrDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CPicAggrDlg)
enum { IDD = IDD_PICAGGR_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPicAggrDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CPicAggrDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButtonSrcBrowse();
afx_msg void OnButtonDestBrowse();
afx_msg void OnButtonAggr();
afx_msg void OnButtonreturn();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
TCHAR m_szDestPath[MAX_PATH];
TCHAR m_szSrcPath[MAX_PATH];
FILEREC m_FileRec;
};
PicAggr.cpp
BOOL CPicAggrDlg::OnInitDialog()
{
CDialog::OnInitDialog();
......
// TODO: Add extra initialization here
memset( m_szSrcPath, 0, sizeof(m_szSrcPath) );
memset( m_szDestPath, 0, sizeof(m_szDestPath) );
memset( &m_FileRec, 0, sizeof(FILEREC) );
return TRUE; // return TRUE unless you set the focus to a control
}
void CPicAggrDlg::OnButtonSrcBrowse()
{
// TODO: Add your control notification handler code here
BROWSEINFO browseinfo;
LPITEMIDLIST lpItemList;
TCHAR szPath[MAX_PATH] = {0};
memset( &browseinfo, 0, sizeof(BROWSEINFO) );
browseinfo.hwndOwner = this->GetSafeHwnd();
browseinfo.pszDisplayName = szPath;
browseinfo.lpszTitle = "请选择文件所在的目录";
if ( lpItemList = SHBrowseForFolder( &browseinfo ) )
{
if ( SHGetPathFromIDList( lpItemList, m_szSrcPath ) )
{
SetDlgItemText( IDC_EDITSRCPATH, m_szSrcPath );
}
else
{
MessageBox( "ERROR:Get Path", NULL, MB_OK );
}
}
else
{
MessageBox( "ERROR:Browse Folder", NULL, MB_OK );
}
}
void CPicAggrDlg::OnButtonDestBrowse()
{
// TODO: Add your control notification handler code here
BROWSEINFO browseinfo;
LPITEMIDLIST lpItemList;
TCHAR szPath[MAX_PATH] = {0};
memset( &browseinfo, 0, sizeof(BROWSEINFO) );
browseinfo.hwndOwner = this->GetSafeHwnd();
browseinfo.pszDisplayName = szPath;
browseinfo.lpszTitle = "请选择文件所在的目录";
if ( lpItemList = SHBrowseForFolder( &browseinfo ) )
{
if ( SHGetPathFromIDList( lpItemList, m_szDestPath ) )
{
SetDlgItemText( IDC_EDITDESTPATH, m_szDestPath );
}
else
{
MessageBox( "ERROR:Get Path", NULL, MB_OK );
}
}
else
{
MessageBox( "ERROR:Browse Folder", NULL, MB_OK );
}
}
void CPicAggrDlg::OnButtonAggr()
{
// TODO: Add your control notification handler code here
WIN32_FIND_DATA findfile;
memset( &findfile, 0, sizeof(WIN32_FIND_DATA) );
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR szPicPath[MAX_PATH];
unsigned int nDestLen = 0;
FILE * fpIndex = NULL;
FILE * fpSrc = NULL;
FILE * fpDest = NULL;
int nRead = 0;
TCHAR szBuf[1024] = {0};
TCHAR szIndexPath[MAX_PATH] = {0};
if ( _tcslen(m_szSrcPath) == 0 )
{
_tcscpy( m_szSrcPath, "." );
}
_tcscat( m_szSrcPath, "//" );
_tcscpy( szPicPath, m_szSrcPath );
_tcscat( szPicPath, "*.*" );
if ( _tcslen(m_szDestPath) == 0 )
{
_tcscpy( m_szDestPath, "." );
}
_tcscat( m_szDestPath, "//" );
_tcscat( m_szDestPath, "LTBFTC" );
_tcscpy( szIndexPath, m_szDestPath );
_tcscat( szIndexPath, ".txt" );
fpIndex = _tfopen( szIndexPath, "a" );
fpDest = _tfopen( m_szDestPath, "wb" );
if ( fpDest == NULL )
{
MessageBox( "ERROR:Open Dest File", NULL, MB_OK );
}
hFind = FindFirstFile( szPicPath, &findfile );
if ( hFind != INVALID_HANDLE_VALUE )
{
if ( findfile.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
{
memset( &m_FileRec, 0, sizeof(FILEREC) );
memset( szPicPath, 0, sizeof(szPicPath) );
_tcscpy( szPicPath, m_szSrcPath );
_tcscat( szPicPath, findfile.cFileName );
fpSrc = _tfopen( szPicPath, "rb" );
if ( fpSrc == NULL )
{
MessageBox( "ERROR:Open Dest File", NULL, MB_OK );
}
_tcscpy( m_FileRec.szFileName, szPicPath );
while ( nRead = fread( szBuf, sizeof(TCHAR), sizeof(szBuf), fpSrc ))
{
fwrite( szBuf, sizeof(TCHAR), nRead, fpDest );
memset( szBuf, 0, sizeof(szBuf) );
m_FileRec.nFileLen += nRead;
}
*(_tcsstr(_tcsrev(m_FileRec.szFileName), "//")) = '/0';
_tcsrev( m_FileRec.szFileName );
nDestLen += m_FileRec.nFileLen;
m_FileRec.nFilePos = nDestLen - m_FileRec.nFileLen;
_ftprintf( fpIndex, "%s,%u,%u/n", m_FileRec.szFileName, m_FileRec.nFilePos, m_FileRec.nFileLen );
nRead = 0;
fflush( fpIndex );
fflush( fpDest );
fclose( fpSrc );
}
}
else
{
MessageBox( "ERROR:Find File", NULL, MB_OK );
}
while ( FindNextFile( hFind, &findfile ) != 0 )
{
if ( findfile.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
{
memset( &m_FileRec, 0, sizeof(FILEREC) );
memset( szPicPath, 0, sizeof(szPicPath) );
_tcscpy( szPicPath, m_szSrcPath );
_tcscat( szPicPath, findfile.cFileName );
fpSrc = _tfopen( szPicPath, "rb" );
if ( fpSrc == NULL )
{
MessageBox( "ERROR:Open Dest File", NULL, MB_OK );
}
_tcscpy( m_FileRec.szFileName, szPicPath );
while ( nRead = fread( szBuf, sizeof(TCHAR), sizeof(szBuf), fpSrc ))
{
fwrite( szBuf, sizeof(TCHAR), nRead, fpDest );
memset( szBuf, 0, sizeof(szBuf) );
m_FileRec.nFileLen += nRead;
}
*(_tcsstr(_tcsrev(m_FileRec.szFileName), "//")) = '/0';
_tcsrev( m_FileRec.szFileName );
nDestLen += m_FileRec.nFileLen;
m_FileRec.nFilePos = nDestLen - m_FileRec.nFileLen;
_ftprintf( fpIndex, "%s,%u,%u/n", m_FileRec.szFileName, m_FileRec.nFilePos, m_FileRec.nFileLen );
nRead = 0;
fflush( fpIndex );
fflush( fpDest );
fclose( fpSrc );
}
}
FindClose( hFind );
fclose( fpDest );
fclose( fpIndex );
CDialog::OnOK();
}
void CPicAggrDlg::OnButtonreturn()
{
// TODO: Add your control notification handler code here
TCHAR szReturn[MAX_FNAME_LEN];
FILE * fpSrc = NULL;
FILE * fpIndex = NULL;
FILE * fpReturn = NULL;
TCHAR szBuf[1024] = {0};
TCHAR szIndex[256];
TCHAR * pPos = NULL;
TCHAR * pLen = NULL;
unsigned int nPos = 0;
unsigned int nLen = 0;
unsigned int nReserve = 1024;
bool bFind = false;
GetDlgItemText( IDC_EDITRETURN, szReturn, sizeof(szReturn) );
if ( _tcslen(szReturn) == 0 )
{
MessageBox( "ERROR:Return File", NULL, MB_OK );
}
_tcscat( m_szDestPath, "//LTBFTC");
fpSrc = _tfopen( m_szDestPath, "rb" );
if ( fpSrc == NULL )
{
MessageBox( "ERROR:Open LTBFTC", NULL, MB_OK );
}
_tcscat( m_szDestPath, ".txt" );
fpIndex = _tfopen( m_szDestPath, "r" );
if ( fpIndex == NULL )
{
MessageBox( "ERROR:Open Index", NULL, MB_OK );
}
TCHAR szTest[256] = {0};
_tcscpy( szTest, _tcscat(_tcsrev(_tcsstr(_tcsrev(m_szDestPath), "//")), szReturn) );
fpReturn = _tfopen( szTest, "wb" );
if ( fpReturn == NULL )
{
MessageBox( "ERROR:Return File", NULL, MB_OK );
}
while ( fgets( szIndex, sizeof(szIndex), fpIndex) )
{
if ( _tcsstr( szIndex, szReturn ) != NULL )
{
bFind = true;
break;
}
}
if ( bFind == true )
{
pPos = _tcsstr( szIndex, "," );
pPos++;
pLen = _tcsstr( pPos, "," );
TCHAR szInt[256] = {0};
_tcsncpy( szInt, pPos, pLen-pPos );
pLen++;
nPos = _ttoi( szInt );
_tcsncpy( szInt, pLen, _tcslen(pLen) );
nLen = _ttoi( szInt );
fseek( fpSrc, nPos, SEEK_SET );
while ( fread( szBuf, sizeof(TCHAR), nReserve, fpSrc ) )
{
fwrite( szBuf, sizeof(TCHAR), sizeof(szBuf), fpReturn );
memset( szBuf, 0, sizeof(szBuf) );
nLen -= nReserve;
if ( nLen < 1024 )
{
nReserve = nLen;
}
}
}
else
{
MessageBox( "ERROR:Not Found", NULL, MB_OK );
}
CDialog::OnOK();
}
*******************************************************************************************************************
PicAggrDlg.h
#define MAX_FNAME_LEN 256 // Max size of field names
typedef struct _FILENAME_LENGTH
{
TCHAR szFileName[MAX_FNAME_LEN];
unsigned int nFilePos;
unsigned int nFileLen;
}FILEREC, * PFILEREC;
/
// CPicAggrDlg dialog
class CPicAggrDlg : public CDialog
{
// Construction
public:
CPicAggrDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CPicAggrDlg)
enum { IDD = IDD_PICAGGR_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPicAggrDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CPicAggrDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButtonSrcBrowse();
afx_msg void OnButtonDestBrowse();
afx_msg void OnButtonAggr();
afx_msg void OnButtonreturn();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
TCHAR m_szDestPath[MAX_PATH];
TCHAR m_szSrcPath[MAX_PATH];
FILEREC m_FileRec;
};