vc关于文件拷贝 解压

单个文件的拷贝

 system  针对单个文件

CopyFile  针对单个文件

 

 

/**
@file_extension egg: .txt .png
**/
void CopyFileToDir(CString source_dir, TCHAR* dest_dir, TCHAR* file_extension) {
CString source, dest;
source.Format("%s*%s", source_dir, file_extension);
dest.Format("%s", dest_dir);
 
SHFILEOPSTRUCT s;
::ZeroMemory(&s, sizeof(s)); // Initialize the structure
 
s.hwnd = NULL;
s.wFunc = FO_COPY;
s.pFrom = source;
s.pTo = dest;
s.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
 
::SHFileOperation(&s);
//可用支持批量处理拷贝,支持单个文件拷贝,
//如果拷贝的目标目录路径或源目录路径里面带下划线,不能正常拷贝,不知道为什么??

 //s.wFunc = FO_MOVE; 如果移动的目标目录路径或源目录路径里面带下划线
//能正常移动文件

-----------------------------------------------------------

 SHFILEOPSTRUCT 这个命令不稳定,不建议使用 ------12.29 4:42:59

 

 
 
//CString cmd;
//char   buffer[MAX_PATH];
//_getcwd(buffer, MAX_PATH);
//cmd.Format("COPY %s %s ", "C:\\Users\\jack\\Desktop\\copy_from\\*.jpg", buffer);
//system(cmd);
//不可用 system仅仅支持文件对文件的拷贝

 

 
 
//system("COPY C:\\Users\\jack\\Desktop\\copyfrom\\*.jpg ./");//不可用,不能使用文件拷贝到目录
//不可用 system仅仅支持文件对文件的拷贝

 

 
system(

"COPY

C:\\Users\\jack\\Desktop\\copyfrom\\1345233921.jpg

C:\\Users\\jack\\Desktop\\copyfrom\\xxxxxxxxxxxxxxxxxxxx.jpg");

//可用 system支持文件对文件的拷贝

 

system("COPY

C:\\Users\\jack\\Desktop\\copy_from\\1345233921.jpg

C:\\Users\\jack\\Desktop\\copy_from\\xxxxxxxxxxxxxxxxxxxx.jpg"); 

 //可用 支持带下划线的目录拷贝

 

 
 
bool bFailIfExists=false;
CopyFile(

"C:\\Users\\jack\\Desktop\\copyfrom\\1345233921.jpg",

"C:\\Users\\jack\\Desktop\\copyfrom\\xxxxxxxxxxxxxxxxxxxx.jpg", bFailIfExists);

//可用 CopyFile只针对文件对文件的处理,不支持批量导入

 

 
 
CopyFile("C:\\Users\\jack\\Desktop\\copyfrom\\1345233921.jpg", "./xxxxxxxxxxxx.jpg", bFailIfExists);
//可用  CopyFile只针对文件对文件的处理,支持当前目录 不支持批量导入

 

bool bFailIfExists=false;
CopyFile("C:\\Users\\jack\\Desktop\\copy_from\\1345233921.jpg", "C:\\Users\\jack\\Desktop\\copy_from\\xxxxxxxxxxxxxxxxxxxx.jpg", bFailIfExists);

 //可用 支持带下划线的目录拷贝

 

 
 
//CopyFile("C:\\Users\\jack\\Desktop\\copyfrom\\1345233921.jpg", "./", bFailIfExists);
//不可用 不支持文件对目录

 

 
 
//CopyFile("C:\\Users\\jack\\Desktop\\copyfrom\\*.jpg", "./xxxxxxxxxxxx.jpg", bFailIfExists);
//不可用 不支持批量处理

 

 
}
 
解压zip文件
头文件
#pragma once



class OZipFile
{
public:
    OZipFile();
    ~OZipFile();
protected:
    
public:
    
    bool UnZipFile(CString strFileName,CString strPassword,CString strOutputPath);
    bool UnZipFileUseRar(CString strFileName, CString strPassword, CString strOutputPath);
    bool UnZipFileUse7Z(CString strFileName, CString strPassword, CString strOutputPath);
    CString Get7ZExePath();
    void CreateFilePath(CString strPath);
    void FormatDirectorys(CString & strPath);
};

文件

#include "stdafx.h"

#include "OZipFile.h"


OZipFile::OZipFile()
{
}


OZipFile::~OZipFile()
{
}



bool OZipFile::UnZipFileUseRar(CString strFileName, CString strPassword, CString strOutputPath)
{
    USES_CONVERSION;
    //CString strRarExePath = CPublicFunction::GetRarExePath();
    CString strRarExePath = GetExePath() + _T("zip\\");
    CString param = _T("e -p");
    param += strPassword;
    param += _T(" ");
    param += strFileName;
    param += _T(" ");
    param += strOutputPath;

    CString lstCmd = strRarExePath + _T(" ");
    lstCmd += param;
    WinExec(T2A(lstCmd),SW_HIDE);
    return false;
}
/*
要求:安装好了7z.exe
@strFileName zip文件的全路径  如C:\01_MyApp\7.zip
@strPassword 
@strOutputPath 输出路径 如 c:\Doc
*/
bool OZipFile::UnZipFileUse7Z(CString strFileName, CString strPassword, CString strOutputPath)
{
    //C:\01_MyApp\7 - Zip\7z.exe x newPack.zip - oc:\Doc - aoa
    USES_CONVERSION;
    CString strRarExePath = Get7ZExePath();//7z.exe的全文件路径
    CString param = _T("x ");
    param += strFileName;
    if (!strPassword.IsEmpty())
    {
        param += _T(" -p");
        param += strPassword;
    }
    param += _T(" -o");
    param += strOutputPath;
    param += _T(" -aos");

    CString lstCmd = strRarExePath + _T(" ");
    lstCmd += param;
    //WinExec(T2A(lstCmd), SW_HIDE);

    STARTUPINFO si = { 0 };
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(STARTUPINFO);
    GetStartupInfo(&si);
    si.wShowWindow = SW_SHOW;
    si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;

    // 运行进程
    PROCESS_INFORMATION pi;
    ZeroMemory(&pi, sizeof(pi));

    TCHAR cmdline[MAX_PATH] = { 0 };
    _tcscpy_s(cmdline, MAX_PATH, lstCmd);
    BOOL bRet = FALSE;
    bRet = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi);

    if (bRet)
    {
        WaitForSingleObject(pi.hProcess, INFINITE);
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
    }
    //int a = GetLastError();
    return false;
}

CString OZipFile::Get7ZExePath()
{
    return GetExePath() + _T("7z.exe");
}


void OZipFile::CreateFilePath(CString strPath)
{
    if (strPath.Find(_T("\\\\")))
    {
        strPath.Replace(_T("\\\\"), _T("\\"));
    }
    if (-1 != strPath.Find(_T("//")))
    {
        strPath.Replace(_T("//"), _T("\\"));
    }
    if (-1 != strPath.Find(_T("////")))
    {
        strPath.Replace(_T("////"), _T("\\"));
    }
    if (-1 != strPath.Find(_T("/")))
    {
        strPath.Replace(_T("/"), _T("\\"));
    }

    int nPos;
    CString strFolder;

    for (nPos = 0; nPos != -1; nPos = strPath.Find(_T("\\")))
    {
        nPos = strPath.Find(_T("\\"));
        strFolder += strPath.Left(nPos);
        if (!PathFileExists(strFolder))
        {
            CreateDirectory(strFolder, NULL);
        }
        strPath = strPath.Mid(nPos + 1);
        strFolder += _T("\\");
    }
}

void OZipFile::FormatDirectorys(CString &strPath)
{
    if (strPath.Find(_T("\\\\")))
    {
        strPath.Replace(_T("\\\\"), _T("\\"));
    }
    if (-1 != strPath.Find(_T("//")))
    {
        strPath.Replace(_T("//"), _T("\\"));
    }
    if (-1 != strPath.Find(_T("////")))
    {
        strPath.Replace(_T("////"), _T("\\"));
    }
    if (-1 != strPath.Find(_T("/")))
    {
        strPath.Replace(_T("/"), _T("\\"));
    }
}

 

 

posted @ 2016-12-27 20:47  balder_m  阅读(929)  评论(0编辑  收藏  举报