Zip a File or a Folder


Microsoft Windows Shell

Most file operations (such as copy a file, delete a file, zip a file, etc.) that the user can perform on a computer using the mouse are provided by a program called Windows Shell. COM allows a program to run the Windows Shell as it will be illustrated in the following problems.
La mayoría de las operaciones (tales como copiar un archivo, borrar un archivo, comprimir en formato zip un archivo, etc.) que el usuario puede realizar en una computadora usando el ratón son de un programa llamado el Shell de Windows. COM permite a un programa ejecutar el Shell de Windows como se ilustra en los siguientes problemas.

Problem 1
Create a Wintempla dialog application called MyZipFile to compress a file. First, we create an empty ZIP file. Second, we add the source file data to the empty ZIP file. In the code, FOF stands for File Operation Flag.
Cree una aplicación de diálogo de Wintempla llamada MyZipFile para comprimir un archivo. Primero, se crea un archivo ZIP vacío. Segundo, se agregan los datos del archivo fuente al archivo ZIP vacío. En el código, FOF quiere decir Bandera de Operación de Archivo.

MyZipFile.h
#pragma once //______________________________________ MyZipFile.h
#include "Resource.h"
class MyZipFile: public Win::Dialog
{
public:
     MyZipFile()
     {
          ::CoInitialize(NULL);
     }
     ~MyZipFile()
     {
          ::CoUninitialize();
     }
protected:
     ...
};


MyZipFile.cpp
...
void MyZipFile::Window_Open(Win::Event& e)
{
     const wchar_t *sourceFile = L"C:\\selo\\image.gif";
     const wchar_t *zipFile = L"C:\\selo\\image.zip";
     //___________________________________________________________ Convert to _variant_t
     _variant_t var_sourceFile(sourceFile);
     _variant_t var_zipFile(zipFile);
     //___________________________________________________________ We create an empty zip file
     if (Sys::FileAssistant::CreateEmptyZipFile(zipFile) == false)
     {
          this->MessageBox(L"Error creating zip file", L"MyZipFile", MB_OK | MB_ICONERROR);
          return;
     }

     IShellDispatchPtr shell;
     FolderPtr destinationFile = NULL;
     _variant_t copyOptions = FOF_NO_UI;// Do not display User Interface(VT_I4, FOF_NO_UI);
     HRESULT hr;
     Com::Exception ex;

     try
     {
          //___________________________________________________________ Get Shell
          hr = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&shell);
          ex.ok(L"CoCreateInstance", hr);
          //___________________________________________________________ Set destination file
          hr = shell->NameSpace(var_zipFile, &destinationFile);
          ex.ok(L"shell->NameSpace", hr);
          //___________________________________________________________ We add the file to the zip file
          hr = destinationFile->CopyHere(var_sourceFile, copyOptions);
          ex.ok(L"destinationFile->CopyHere", hr);
          if (hr == S_FALSE)
          {
               this->MessageBox(L"The source file does not exist", L"MyZipFile", MB_OK | MB_ICONERROR);
               return;
          }
          //___________________________________________________________ Wait for the copy thread to complete
          Sleep(1000);
     }
     catch(Com::Exception& excep)
     {
          excep.Display(hWnd, L"MyZipFile");
     }
     catch(_com_error excep)
     {
          Com::Exception::Display(hWnd, excep, L"MyZipFile");
     }
}


Problem 2
Create a Wintempla dialog application called MyZipFolder to compress a folder using the ZIP format. First, we create an empty ZIP file. Finally, we add data to the empty ZIP file.
Cree una aplicación de diálogo de Wintempla llamada MyZipFolder para comprimir un folder en formato ZIP. Primero, se crea un archivo ZIP vacío. Finalmente, se agregan los datos al archivo ZIP vacío.

MyZipFolder.h
#pragma once //______________________________________ MyZipFolder.h
#include "Resource.h"

class MyZipFolder: public Win::Dialog
{
public:
     MyZipFolder()
     {
          ::CoInitialize(NULL);
     }
     ~MyZipFolder()
     {
          ::CoUninitialize();
     }
...
};


MyZipFolder.cpp
...
void MyZipFolder::Window_Open(Win::Event& e)
{
     const wchar_t *sourceFolder = L"C:\\selo\\110";
     const wchar_t *zipFile = L"C:\\selo\\110data.zip";
     //___________________________________________________________ Convert to _variant_t
     _variant_t var_sourceFolder(sourceFolder);
     _variant_t var_zipFile(zipFile);
     //___________________________________________________________ We create an empty zip file
     if (Sys::FileAssistant::CreateEmptyZipFile(zipFile) == false)
     {
          this->MessageBox(L"Error creating zip file", L"MyZipFile", MB_OK | MB_ICONERROR);
          return;
     }

     IShellDispatchPtr shell;
     FolderPtr folderSource = NULL;
     FolderPtr folderDestination = NULL;
     FolderItemsPtr folderItems = NULL;
     _variant_t copyOptions = FOF_NO_UI;// Do not display User Interface(VT_I4, FOF_NO_UI);
     HRESULT hr;
     Com::Exception ex;

     try
     {
          //___________________________________________________________ CoCreateInstance
          hr = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&shell);
          ex.ok(L"CoCreateInstance", hr);
          //___________________________________________________________ Set destination file
          hr = shell->NameSpace(var_zipFile, &folderDestination);
          if (FAILED(hr))
          {
               this->MessageBox(L"The destination file name is incorrect", L"MyZipFile", MB_OK | MB_ICONERROR);
               return;
          }
          //___________________________________________________________ Set source folder
          hr = shell->NameSpace(var_sourceFolder, &folderSource);
          if (FAILED(hr))
          {
               this->MessageBox(L"The source file does not exist", L"MyZipFolder", MB_OK | MB_ICONERROR);
               return;
          }
          //___________________________________________________________ Get the items in the folder
          folderSource->Items(&folderItems);
          //___________________________________________________________ We add the files to the zip file
          _variant_t var_folderItems((IDispatch*)folderItems, true);
          hr = folderDestination->CopyHere(var_folderItems, copyOptions);
          ex.ok(L"folderDestination->CopyHere", hr);
          if (hr == S_FALSE)
          {
               this->MessageBox(L"Unable to compress folder", L"MyZipFolder", MB_OK | MB_ICONERROR);
               return;
          }
          //___________________________________________________________ Wait for the copy thread to complete
          Sleep(1000);
     }
     catch(Com::Exception& excep)
     {
          excep.Display(hWnd, L"MyZipFolder");
     }
     catch(_com_error excep)
     {
          Com::Exception::Display(hWnd, excep, L"MyZipFolder");
     }
}


Wintempla ZIP

Wintempla provides the functions Sys::FileAssistant::ZipFile and Sys::FileAssistant::ZipFolder to compress files and folders respectively.
Wintempla proporciona las funciones Sys::FileAssistant::ZipFile y Sys::FileAssistant::ZipFolder para comprimir archivos y carpetas respectivamente.

© Copyright 2000-2019 Wintempla selo. All Rights Reserved. Sep 05 2019. Home