* Copyright (c) 2022-2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_H
#define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_H
#include <fcntl.h>
#include <functional>
#include <iostream>
#include <memory>
#include <sys/stat.h>
#include <time.h>
#include <vector>
#include "file_path.h"
#include "zip_utils.h"
#include "zlib_callback_info_base.h"
namespace OHOS {
namespace AppExecFwk {
namespace LIBZIP {
class WriterDelegate;
class FileAccessor {
public:
virtual ~FileAccessor() = default;
struct DirectoryContentEntry {
DirectoryContentEntry(const FilePath &path, bool directory) : path(path), isDirectory(directory)
{}
FilePath path;
bool isDirectory = false;
};
};
class ZipParams {
public:
ZipParams(const std::vector<FilePath> &srcDir, const FilePath &destFile);
ZipParams(const std::vector<FilePath> &srcDir, int destFd);
virtual ~ZipParams()
{}
int DestFd() const
{
return destFd_;
}
const std::vector<FilePath> &SrcDir() const
{
return srcDir_;
}
const FilePath &DestFile() const
{
return destFile_;
}
void SetFilesTozip(const std::vector<std::pair<FilePath, FilePath>> &srcRelativePaths)
{
srcFiles_ = srcRelativePaths;
}
const std::vector<std::pair<FilePath, FilePath>> &GetFilesTozip() const
{
return srcFiles_;
}
using FilterCallback = std::function<bool(const FilePath &)>;
void SetFilterCallback(FilterCallback filterCallback)
{
filterCallback_ = filterCallback;
}
const FilterCallback &GetFilterCallback() const
{
return filterCallback_;
}
void SetIncludeHiddenFiles(bool includeHiddenFiles)
{
includeHiddenFiles_ = includeHiddenFiles;
}
bool GetIncludeHiddenFiles() const
{
return includeHiddenFiles_;
}
void SetFileAccessor(std::unique_ptr<FileAccessor> file_accessor)
{
fileAccessor_ = std::move(file_accessor);
}
FileAccessor *GetFileAccessor() const
{
return fileAccessor_.get();
}
private:
std::vector<FilePath> srcDir_;
FilePath destFile_;
int destFd_ = kInvalidPlatformFile;
std::vector<std::pair<FilePath, FilePath>> srcFiles_;
FilterCallback filterCallback_;
bool includeHiddenFiles_ = true;
std::unique_ptr<FileAccessor> fileAccessor_;
};
bool Zip(const std::string &srcPath, const std::string &destPath, const OPTIONS &options,
bool includeHiddenFiles, std::shared_ptr<ZlibCallbackInfoBase> zlibCallbackInfo);
bool Zips(const std::vector<std::string> &srcFiles, const std::string &destPath, const OPTIONS &options,
bool includeHiddenFiles, std::shared_ptr<ZlibCallbackInfoBase> zlibCallbackInfo);
bool Unzip(const std::string &srcFile, const std::string &destFile, const OPTIONS options,
std::shared_ptr<ZlibCallbackInfoBase> zlibCallbackInfo);
ErrCode GetOriginalSize(const std::string &srcFile, int64_t &originalSize);
}
}
}
#endif