티스토리 뷰
C에서 호출하여 Zip 압축
zip압축 파이썬 코드 : http://petra.tistory.com/1236
MFC관련 사전 준비 작업 : http://petra.tistory.com/1235
<pzip.py 코드>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
'''pzip.py
c:\Data\Data.txt 를 c:\Data\Data.zip 에 압축하기
ppzip('c:\\Data', 'Data.txt', 'Data.zip')
파일이 이미 존재하면 덮어쓰기가 되고, 9GB 파일도 압축 가능하다.(32비트 프로그램에서 확인 안됨)'''
import zipfile
import os
'''파일 단위 압축'''
def ppzip(path, sourceFile, destFile):
oldPath = os.getcwd()
os.chdir(path)
wzip = zipfile.ZipFile(destFile, mode = 'w')
wzip.write(sourceFile, '.\\', compress_type = zipfile.ZIP_DEFLATED)
wzip.close()
os.chdir(oldPath)
'''폴더단위 압축, 아직 확인 안된 코드임'''
def MultiZip():
fantasy_zip = zipfile.ZipFile('C:\\Stories\\Fantasy\\archive.zip', 'w')
for folder, subfolders, files in os.walk('C:\\Stories\\Fantasy'):
for file in files:
if file.endswith('.pdf'):
fantasy_zip.write(os.path.join(folder, file), os.path.relpath(os.path.join(folder,file), 'C:\\Stories\\Fantasy'), compress_type = zipfile.ZIP_DEFLATED)
fantasy_zip.close() |
cs |
<MFC 코드>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 |
//================ 파이썬36
#ifdef _DEBUG
#pragma comment(lib, "Python36/python36_d.lib") // 배포할떄는 문제 없나???
#else
#pragma comment(lib, "Python36/python36.lib")
#endif
#include "Python36/Python.h"
//================
TCHAR* Append_Script_Path() // 파이썬 모듈을 검색할 폴더에 Script 폴더를 추가한다.
{
TCHAR szCurrentDir[MAX_PATH] = { 0, };
TCHAR szScriptDir[MAX_PATH] = { 0, };
GetCurrentDirectory(MAX_PATH, szCurrentDir);
//_stprintf_s(szScriptDir, MAX_PATH, _T("%s\\Script"), szCurrentDir);
_stprintf_s(szScriptDir, MAX_PATH, _T("C:\\Users\\USER\\Documents\\Python"), szCurrentDir);
PySys_SetPath(szScriptDir);
return szScriptDir;
/*
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\"/path/to/python/module/here\")");
*/
}
void DontWriteByteCode(bool bYes)
{
if (true == bYes)
PySys_SetObject("dont_write_bytecode", Py_True);
else
PySys_SetObject("dont_write_bytecode", Py_False);
}
void* CMFCRESTTelegramDlg::문자열코드변환(CString str)
{
PyObject* pPyObject = NULL;
#ifdef _UNICODE
pPyObject = PyUnicode_FromUnicode(str, str.GetLength());
#else
USES_CONVERSION;
wchar_t* wString = T2W(str.GetBuffer(0));
pPyObject = PyUnicode_FromUnicode(wString, wcslen(wString));
#endif
return (void*)pPyObject;
}
void CMFCRESTTelegramDlg::OnBnClickedButtonZip()
{
// TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
PyObject* pModule = NULL;
PyObject* pArgument = NULL;
PyObject* pReturn = NULL;
PyObject* pZip = NULL;
Py_Initialize(); // Lib 초기화
//Append_Script_Path();
DontWriteByteCode(true); // 바이너리코드 저장여부
pModule = PyImport_ImportModule("pzip"); // 파이썬 로딩
if (NULL == pModule)
{
PyErr_Print();
AfxMessageBox(TEXT("파이썬 코드 불러오기 실패!!!"));
Py_Finalize(); // Lib 해제
return;
}
pZip = PyObject_GetAttrString(pModule, "ppzip"); // 함수 얻기
if (NULL == pZip)
{
PyErr_Print();
AfxMessageBox(TEXT("파이썬 함수 찾기 실패!!!"));
if (NULL != pModule)
Py_DECREF(pModule); // 파이썬 리소스 해제
Py_Finalize(); // Lib 해제
return;
}
CString strPath = TEXT("C:\\Users\\USER\\Documents\\Python");
CString strSourceFile = TEXT("DataLog데이터.txt"); //
CString strDestFile = TEXT("DataLog데이터.zip");
PyObject* pPath = (PyObject*)문자열코드변환(strPath);
PyObject* pSourceFile = (PyObject*)문자열코드변환(strSourceFile);
PyObject* pDestFile = (PyObject*)문자열코드변환(strDestFile);
//pArgument = PyTuple_Pack(3, PyUnicode_FromFormat("%s", pszToken), PyUnicode_FromFormat("%s", pszUser), PyUnicode_FromFormat("%s", strMsg)); // 한글깨짐
pArgument = PyTuple_Pack(3, pPath, pSourceFile, pDestFile); // MFC 프로젝트가 유니코드/멀티바이트 상관없이 한글 파일명은 안깨진다.
pReturn = PyObject_CallObject(pZip, pArgument);
if (pArgument != NULL)
Py_DECREF(pArgument);
pArgument = NULL;
//pReturn = PyObject_CallObject(pSendSMS, NULL); // 인수 없을때
if (Py_None != pReturn) // 리턴없을때
{
PyErr_Print();
AfxMessageBox(TEXT("파이썬 함수 호출 실패!!!"));
}
if (NULL != pModule)
Py_DECREF(pModule); // 파이썬 리소스 해제
if (pArgument != NULL)
Py_DECREF(pArgument);
if (pReturn != NULL)
Py_DECREF(pReturn);
if (pZip != NULL)
Py_DECREF(pZip);
Py_Finalize(); // Lib 해제
} |
cs |
<실행>
DataLog데이터.txt (9GB) 파일이 DataLog데이터.zip 폴더로 압축된다.