Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
lumiscosity committed Sep 30, 2024
1 parent 198507d commit dca3cc6
Show file tree
Hide file tree
Showing 10 changed files with 10,035 additions and 2 deletions.
1 change: 0 additions & 1 deletion third_party/KZip
Submodule KZip deleted from 31df0f
1 change: 0 additions & 1 deletion third_party/zip
Submodule zip deleted from 8e657b
3 changes: 3 additions & 0 deletions third_party/zip/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/build*
*.zip
*.txt
9,253 changes: 9,253 additions & 0 deletions third_party/zip/3rdparty/miniz-3.0.2/miniz.h

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions third_party/zip/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 John Shieh <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 51 additions & 0 deletions third_party/zip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

## Introduction

A C++20 library offering a clean and simple interface to the miniz library for creating, reading, and updating ZIP files.

## Examples

```cpp
#include <iostream>
#include "zip.hpp"
#include "exceptions.hpp"

int main()
{
try
{
minidocx::Zip z;

// Create a new zip archive.
z.open("foo.zip", minidocx::Zip::OpenMode::Create);
z.addFileFromString("a.txt", "aaa");
z.addFileFromString("dir/b.txt", "bbb");
z.addFileFromString("dir/subdir/c.txt", "ccc");
z.addFileFromString("dir/subdir/d.txt", "ddd");
z.close();

// Read the existing zip archive.
z.open("foo.zip", minidocx::Zip::OpenMode::ReadOnly);
std::cout << z.extractFileToString("a.txt") << std::endl;
z.extractFileToDisk("dir/b.txt", "b.txt");
z.close();

// Update the existing zip archive.
z.open("foo.zip", minidocx::Zip::OpenMode::Update);
z.deleteFiles({ "a.txt", "dir/subdir/c.txt" });
z.addFileFromDisk("readme.md", "README.md");
z.close();
}
catch (const minidocx::exception& ex)
{
std::cerr << ex.what() << std::endl;
}

return 0;
}
```

Note:

- No need to add directories manually unless you really want to add an empty directory. In this case, call the `create_directory()` method with an directory name ending in a forwardslash `/`.
- If you delete a directory, it will also delete all files and directories in that directory.
39 changes: 39 additions & 0 deletions third_party/zip/include/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

#pragma once

#include <stdexcept>
#include <string>


namespace minidocx
{
class exception : public std::runtime_error
{
public:
exception(const std::string& message, const std::string& sender = "minidocx");
};

class unsupported_feature : public exception
{
public:
unsupported_feature();
};

class invalid_parameter : public exception
{
public:
invalid_parameter();
};

class invalid_operation : public exception
{
public:
invalid_operation();
};

class io_error : public exception
{
public:
io_error(const std::string& filename, const std::string& message);
};
}
117 changes: 117 additions & 0 deletions third_party/zip/include/zip.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

#pragma once

#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include <utility>
#include <chrono>
#include <cstdint>


namespace minidocx
{
namespace fs = std::filesystem;

class Zip
{
public:
Zip();
~Zip();

Zip(const Zip& src) = delete;
Zip& operator=(const Zip& rhs) = delete;

Zip(Zip&& src) noexcept;
Zip& operator=(Zip&& rhs) noexcept;


enum class OpenMode { None, ReadOnly, Update, Create, Create64 };

// Opens an archive using given mode.
void open(const std::string& filename, const OpenMode openMode = OpenMode::ReadOnly);

// Closes the archive and releases resources.
void close() noexcept;

// Returns true if the archive is in zip64 format.
bool isZip64() const;


// High-level helpers


public:
// Extracts file or folder to disk.
void extractTo(const fs::path& name, const fs::path& dst);

// Adds file or folder from disk.
void add(const fs::path& name, const fs::path& src);

// Deletes files and/or folders from the archive.
void deleteFiles(const std::vector<fs::path>& names);


// Low-level methods


public:
// Returns the total number of entries in the archive.
size_t countEntries() const;

// List of all entries.
std::vector<fs::path> listEntries();

// Returns false if the entry cannot be found.
bool hasEntry(const fs::path& name);

// Returns the uncompressed size of entry in bytes.
size_t entrySize(const fs::path& name);


public:
// Extracts an file into output stream and returns its modified time.
std::chrono::system_clock::time_point
extractFileToStream(const fs::path& name, std::ostream& dst);

// Extracts an file into disk file and sets its modified time.
void extractFileToDisk(const fs::path& name, const fs::path& dst);

// Extracts an file and returns a string.
std::string extractFileToString(const fs::path& name);


public:
// Adds an file from input stream and records its modified time.
void addFileFromStream(const fs::path& name, std::istream& src,
const std::chrono::system_clock::time_point& modified_time = std::chrono::system_clock::now());

// Adds an file from disk file and records its modified time.
void addFileFromDisk(const fs::path& name, const fs::path& src);

// Adds an file from string.
void addFileFromString(const fs::path& name, const std::string& data);

// Create an empty folder in the archive.
void addFolder(const fs::path& name);


public:
inline void swap(Zip& src) noexcept
{
std::swap(zip_, src.zip_);
std::swap(file_, src.file_);
std::swap(filename_, src.filename_);
std::swap(openMode_, src.openMode_);
}

private:
struct mz_zip_archive_ex;

mz_zip_archive_ex* zip_{ nullptr };
std::fstream file_;
std::string filename_;
OpenMode openMode_{ OpenMode::None };
};
}
31 changes: 31 additions & 0 deletions third_party/zip/src/exceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#include "../include/exceptions.hpp"


namespace minidocx
{
exception::exception(const std::string& message, const std::string& sender)
: runtime_error{ sender + ": " + message}
{
}

unsupported_feature::unsupported_feature()
: exception("unsupported feature")
{
}

invalid_parameter::invalid_parameter()
: exception("invalid parameter")
{
}

invalid_operation::invalid_operation()
: exception("invalid operation")
{
}

io_error::io_error(const std::string& filename, const std::string& message)
: exception(message + ": '" + filename + "'")
{
}
}
Loading

0 comments on commit dca3cc6

Please sign in to comment.