C++ How to read a zipfile into bytes

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật