Visual Studio C++ compression API compiles, and compresses a single file successfully, and uncompressing the file with Extraction Wizard give the error in the attached screengrab:
Am I missing something to compress the single file correctly so that decompression will work in File Explorer?
Thanks for you help.
Windows 11 Visual Studio C++ 2022 Desktop Target Intel Core i9
Code from Windows website: Using the Compression API in buffer mode was used, and a jpg (HDMI.jpg) was compressed using code to create a zipped file called Myzip.zip. The input file size was 104269 bytes, and the compressed size decreased to 100676.
I expected that unzipping the file would create an uncompressed file called HDMI.jpg, and instead I received the error Cannot Complete the Compressed (zipped) Folders Extraction Wizard The compressed (zipped) folder is empty. Before you can extract files, you must copy files to this compressed (zipped) folder.
There is a WriteFile function, which seemed successful. This was tried in both Debug (x64) mode and Release (x64) mode. Running the final program in the IDE or the Command Prompt provided the same issue. I also tried to use different algorithms such as COMPRESS_ALGORITHM_XPRESS_HUFF and COMPRESS_ALGORITHM_MSZIP in the parameter for CreateCompressor and all had the same error when extracting.
An uncompressed jpg file was tried, and a txt file was also tried, both with the same error when attempting to unzip.
Also tried 7-zip and it had the same error.
#include <Windows.h>
#include <stdio.h>
#include <compressapi.h>
void wmain(_In_ int argc, _In_ WCHAR* argv[])
{
COMPRESSOR_HANDLE Compressor = NULL;
PBYTE CompressedBuffer = NULL;
PBYTE InputBuffer = NULL;
HANDLE InputFile = INVALID_HANDLE_VALUE;
HANDLE CompressedFile = INVALID_HANDLE_VALUE;
BOOL DeleteTargetFile = TRUE;
BOOL Success;
SIZE_T CompressedDataSize, CompressedBufferSize;
DWORD InputFileSize, ByteRead, ByteWritten;
LARGE_INTEGER FileSize;
ULONGLONG StartTime, EndTime;
double TimeDuration;
if (argc != 3)
{
wprintf(L"Usage:\n\t%s <input_file_name> <compressd_file_name>\n", argv[0]);
return;
}
// Open input file for reading, existing file only.
InputFile = CreateFile(
argv[1], // Input file name
GENERIC_READ, // Open for reading
FILE_SHARE_READ, // Share for read
NULL, // Default security
OPEN_EXISTING, // Existing file only
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL); // No attr. template
if (InputFile == INVALID_HANDLE_VALUE)
{
wprintf(L"Cannot open \t%s\n", argv[1]);
goto done;
}
// Get input file size.
Success = GetFileSizeEx(InputFile, &FileSize);
if ((!Success) || (FileSize.QuadPart > 0xFFFFFFFF))
{
wprintf(L"Cannot get input file size or file is larger than 4GB.\n");
goto done;
}
InputFileSize = FileSize.LowPart;
// Allocate memory for file content.
InputBuffer = (PBYTE)malloc(InputFileSize);
if (!InputBuffer)
{
wprintf(L"Cannot allocate memory for uncompressed buffer.\n");
goto done;
}
// Read input file.
Success = ReadFile(InputFile, InputBuffer, InputFileSize, &ByteRead, NULL);
if ((!Success) || (ByteRead != InputFileSize))
{
wprintf(L"Cannot read from \t%s\n", argv[1]);
goto done;
}
// Open an empty file for writing, if exist, overwrite it.
CompressedFile = CreateFile(
argv[2], // Compressed file name
GENERIC_WRITE | DELETE, // Open for writing; delete if cannot compress
0, // Do not share
NULL, // Default security
CREATE_ALWAYS, // Create a new file; if exist, overwrite it
FILE_ATTRIBUTE_NORMAL, // Normal file
NULL); // No template
if (CompressedFile == INVALID_HANDLE_VALUE)
{
wprintf(L"Cannot create file \t%s\n", argv[2]);
goto done;
}
// Create an XpressHuff compressor.
Success = CreateCompressor(
COMPRESS_ALGORITHM_XPRESS_HUFF, // Compression Algorithm
NULL, // Optional allocation routine
&Compressor); // Handle
if (!Success)
{
wprintf(L"Cannot create a compressor %d.\n", GetLastError());
goto done;
}
// Query compressed buffer size.
Success = Compress(
Compressor, // Compressor Handle
InputBuffer, // Input buffer, Uncompressed data
InputFileSize, // Uncompressed data size
NULL, // Compressed Buffer
0, // Compressed Buffer size
&CompressedBufferSize); // Compressed Data size
// Allocate memory for compressed buffer.
if (!Success)
{
DWORD ErrorCode = GetLastError();
if (ErrorCode != ERROR_INSUFFICIENT_BUFFER)
{
wprintf(L"Cannot compress data: %d.\n", ErrorCode);
goto done;
}
CompressedBuffer = (PBYTE)malloc(CompressedBufferSize);
if (!CompressedBuffer)
{
wprintf(L"Cannot allocate memory for compressed buffer.\n");
goto done;
}
}
StartTime = GetTickCount64();
// Call Compress() again to do real compression and output the compressed
// data to CompressedBuffer.
Success = Compress(
Compressor, // Compressor Handle
InputBuffer, // Input buffer, Uncompressed data
InputFileSize, // Uncompressed data size
CompressedBuffer, // Compressed Buffer
CompressedBufferSize, // Compressed Buffer size
&CompressedDataSize); // Compressed Data size
if (!Success)
{
wprintf(L"Cannot compress data: %d\n", GetLastError());
goto done;
}
EndTime = GetTickCount64();
// Get compression time.
TimeDuration = (EndTime - StartTime) / 1000.0;
// Write compressed data to output file.
Success = WriteFile(
CompressedFile, // File handle
CompressedBuffer, // Start of data to write
CompressedDataSize, // Number of byte to write
&ByteWritten, // Number of byte written
NULL); // No overlapping structure
if ((ByteWritten != CompressedDataSize) || (!Success))
{
wprintf(L"Cannot write compressed data to file: %d.\n", GetLastError());
goto done;
}
wprintf(
L"Input file size: %d; Compressed Size: %d\n",
InputFileSize,
CompressedDataSize);
wprintf(L"Compression Time(Exclude I/O): %.2f seconds\n", TimeDuration);
wprintf(L"File Compressed.\n");
DeleteTargetFile = FALSE;
done:
if (Compressor != NULL)
{
CloseCompressor(Compressor);
}
if (CompressedBuffer)
{
free(CompressedBuffer);
}
if (InputBuffer)
{
free(InputBuffer);
}
if (InputFile != INVALID_HANDLE_VALUE)
{
CloseHandle(InputFile);
}
if (CompressedFile != INVALID_HANDLE_VALUE)
{
// Compression fails, delete the compressed file.
if (DeleteTargetFile)
{
FILE_DISPOSITION_INFO fdi;
fdi.DeleteFile = TRUE; // Marking for deletion
Success = SetFileInformationByHandle(
CompressedFile,
FileDispositionInfo,
&fdi,
sizeof(FILE_DISPOSITION_INFO));
if (!Success) {
wprintf(L"Cannot delete corrupted compressed file.\n");
}
}
CloseHandle(CompressedFile);
}
}
Win32 APIs do not provide way to create zip format files.
You need to use third-party library or create the format by yourself.
Maybe similar: https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile?view=net-6.0
To use the ZipFile class in a .NET Framework app, you must add a reference to the System.IO.Compression.FileSystem assembly in your project.
#include<iostream>
#include<string>
using namespace System;
using namespace System::IO;
using namespace System::IO::Compression;
System::String^ StdStringToUTF16(std::string s)
{
cli::array<System::Byte>^ a = gcnew cli::array<System::Byte>(s.length());
int i = s.length();
while (i-- > 0)
{
a[i] = s[i];
}
return System::Text::Encoding::UTF8->GetString(a);
}
int main()
{
std::string startPath = "FolderName";
String^ zipPath = ".\\result1.zip";
String^ extractPath = ".\\extract";
ZipFile::CreateFromDirectory(StdStringToUTF16(startPath), zipPath);
ZipFile::ExtractToDirectory(zipPath, extractPath);
return 0;
}