How can I read a zip file into bytes?
Here is a similar topic, but the outcome of the examples in this topic seems incorrect compared to the outcome of my Python script (see below), which seems correct.
I use this zip file as example: https://getsamplefiles.com/download/zip/sample-1.zip
C++ program:
#include <iostream>
#include <vector>
#include <fstream>
using byte = unsigned char;
std::vector<char> static ReadFileBytes1(const std::string& fileName)
{
std::ifstream ifs;
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ifs.open(fileName.c_str(), std::ifstream::binary | std::ifstream::ate);
std::ifstream::pos_type size = ifs.tellg();
ifs.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
ifs.read(buffer.data(), size);
return buffer;
}
std::vector<byte> static ReadFileBytes2(const std::string& fileName)
{
std::ifstream ifs;
ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit);
ifs.open(fileName.c_str(), std::ifstream::binary | std::ifstream::ate);
std::ifstream::pos_type size = ifs.tellg();
ifs.seekg(0, std::ios::beg);
std::vector<byte> buffer(size);
ifs.read(buffer.data(), size); // This does not work. Argument of type byte * is incompatible with parameter of type char*
return buffer;
}
int main()
{
std::vector<char> zipFileInBytes1 = ReadFileBytes1("C:\Users\admin\Desktop\sample-1.zip");
std::vector<byte> zipFileInBytes2 = ReadFileBytes2("C:\Users\admin\Desktop\sample-1.zip");
system("pause");
}
Python script:
# Execute like this:
# "python configuration.py https://getsamplefiles.com/download/zip/sample-1.zip C:UsersadminDesktop"
import io, json, requests, sys, zipfile
def make_request(url, destination):
try:
response = requests.get(url)
if response.ok == False or 'error' in response.text:
return { "success": False, "message": response.text }
print(response.content)
zipStream = zipfile.ZipFile(io.BytesIO(response.content))
zipStream.extractall(destination)
return { "success": True, "message": f"Files fetched and successfully extracted to {destination}" }
except requests.ConnectionError as e:
return { "success": False, "message": f"Failed to connect to {url}" }
except requests.Timeout as e:
return { "success": False, "message": "Timeout error" }
except requests.RequestException as e:
return { "success": False, "message": str(e) }
# Command-line arguments
endpoint = sys.argv[1]
destination = sys.argv[2]
# Get zipfile and extract at destination path
getExtractZip = make_request(endpoint, destination)
if getExtractZip["success"] == False:
sys.exit(f"Failed to get and extract zipped files. Message: {getExtractZip['message']}")
print("Successfully extracted zip file")
The print(response.content) output of my Python script seems to be a correct byte representation of the zip file. The variables in my C++ program seems to store a different result which seems incorrect. I would paste both results, but they are huge, so I think its better to run the C++ program and the Python script yourself to see the difference.
Sidenote: Eventually I would like to serve the zip file bytes (Content-Type: application/zip) to a client via an HTTP server, but that is out of scope for now.