| Name | Date | Size | #Lines | LOC | ||
|---|---|---|---|---|---|---|
| .. | - | - | ||||
| AppScope/ | 06-Mar-2025 | - | 36 | 33 | ||
| entry/ | 06-Mar-2025 | - | 2,592 | 2,421 | ||
| hvigor/ | 06-Mar-2025 | - | 24 | 22 | ||
| screenshots/device/ | 06-Mar-2025 | - | ||||
| .gitignore | D | 06-Mar-2025 | 121 | 10 | 10 | |
| README.md | D | 06-Mar-2025 | 1.3 KiB | 36 | 18 | |
| README_zh.md | D | 06-Mar-2025 | 7.8 KiB | 158 | 94 | |
| build-profile.json5 | D | 06-Mar-2025 | 1.1 KiB | 43 | 42 | |
| hvigorfile.ts | D | 06-Mar-2025 | 158 | 2 | 1 | |
| hvigorw | D | 06-Mar-2025 | 2.1 KiB | 62 | 28 | |
| hvigorw.bat | D | 06-Mar-2025 | 2 KiB | 73 | 56 | |
| oh-package.json5 | D | 06-Mar-2025 | 812 | 27 | 25 | |
| ohosTest.md | D | 06-Mar-2025 | 1.6 KiB | 15 | 13 |
README.md
1# Zip and Unzip 2 3### Introduction 4 5This sample demonstrates the use of @ohos.zlib. Below shows the sample app. 6 7 8 9### Concepts 10 11Zip: compresses a file or folder based on the passed file path to zip and the path of the zipped file. 12 13Unzip: decompresses a file or folder based on the passed file path to unzip and the path of the unzipped file. 14 15### Required Permissions 16 17N/A 18 19### Usage 20 211. Touch **+** in the upper right corner of the sample app. 22 232. In the **Create file** dialog box displayed, enter the file name and content, and touch **OK**. 24 253. The file created is displayed on the home page. Touch **Zip**. A message is displayed, indicating that the file is compressed, and a **.zip** file with the same name is created in the file list. 26 274. Touch **Unzip** next to the zipped file. A message is displayed, indicating that the file is decompressed, and a folder with the same name is created in the file list. 28 29### Constraints 30 311. This sample can only be run on standard-system devices. 32 332. This sample requires DevEco Studio 3.1 Canary1 (Build Version: 3.1.0.100, built on November 3, 2022) to compile and run. 34 353. The paths passed in the `Zip` and `Unzip` APIs must be the relative paths obtained by the context API. Currently, `Zip` and `Unzip` support operations only on files in the app directory. 36
README_zh.md
1# 压缩与解压 2 3### 介绍 4 5本示例通过[@ohos.zlib](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-zlib.md) 6和[@ohos.fileio](https://gitee.com/openharmony/docs/blob/OpenHarmony-5.0.1-Release/zh-cn/application-dev/reference/apis-core-file-kit/js-apis-fileio.md) 接口,实现添加文件、解压和压缩文件场景。 7 8### 效果预览 9 10| 主页面 | zlib解压缩文件页面 | zlib相关接口调用页面 | 压缩多个文件页面 | 11| :----------------------------------------------------------: | :----------------------------------------------------------: | :---------------------------------------------------------: | :---------------------------------------------------------: | 12| <img src="./screenshots/device/Screenshot_20231208091553194.jpeg" /> | <img src="./screenshots/device/Screenshot_20231208091651506.jpeg" /> |  |  | 13 14使用说明 15 16zlib解压缩文件 17 181.点击屏幕右上角 **+** 按钮,弹出创建文件窗口。 19 202.输入文件名称、文件内容,并点击 **确定** 按钮来创建文件。 21 223.文件创建成功后,文件名称自动追加.txt后缀并在主页面列表会显示,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/files/,点击 **压缩** 按钮,提示“文件压缩成功”,并会在文件列表创建一个相同名称的.zip文件。 23 244.点击压缩文件后的 **解压** 按钮,提示“文件解压成功”,并会再次在文件列表创建一个同名文件夹。 25 26 27 28压缩多个文件使用说明: 29 301.主界面点击**zLib相关接口调用**按钮,进入 zLib相关接口调用界面。 31 322.点击屏幕上方**压缩多个文件**,跳转到压缩多个文件界面。 33 343.点击屏幕右上角的 **+**按钮,弹出创建文件窗口。 35 364.输入文件名称、文件内容,并点击 **确定** 按钮来创建文件,重复操作,创建多个文件。 37 385.文件创建成功后,文件名称自动追加.txt后缀并在主页面列表会显示,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/files/, 39 40点击 **压缩** 按钮,提示“压缩多个文件成功”,并会在文件列表创建一个名称为test.zip的文件。 41 426.点击屏幕右上角的 **删除**按钮,弹出删除窗口,点击 **确定** 按钮会删除列表中所有.zip文件。 43 44 45 46gzip格式解压缩使用说明: 47 481.主界面点击**zLib相关接口调用**按钮,进入zLib相关接口调用界面。 49 502.zLib相关接口调用界面中点击**gzip格式解压缩**按钮,提示“gzip文件压缩成功,压缩数据为:hello,hello!",数据压入gzipTest.gz文件中,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/cache/。 51 523.等待约3s后,提示“gzip文件解压成功,解压数据为:hello,hello!",并将解压后的结果写入gzipTest.txt文件中,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/cache/。 53 54 55 56zlib格式解压缩使用说明: 57 581.主界面点击**zLib相关接口调用**按钮,进入zLib相关接口调用界面。 59 602.zLib相关接口调用界面中点击**zlib格式解压缩**按钮,提示“zlib文件压缩成功,压缩数据为: hello,hello!",并将压缩后的结果写入zlibDeflateFile.txt文件中,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/cache/。 61 623.等待约3s后,提示“zlib文件解压成功,解压数据为: hello,hello!",并将解压后的结果写入zlibInflateFile.txt文件中,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/cache/。 63 64 65### 工程目录 66 67``` 68entry/src/main/ets/ 69|---common 70| |---AddDialog.ets // 弹窗组件 71|---entryability 72|---model 73| |---DataSource.ets // 懒加载文件 74| |---Logger.ts // 日志文件 75|---zlib 76| |---CompressFilesPage.ets // 压缩多个文件 77| |---ZipLibDecompressionPage.ets // ZipLib解压缩界面 78| |---ZlibInterfaceListPage.ets // 接口分类界面,包含zlib解压缩、gzip解压缩、crc校验功能 79|---pages 80| |---Index.ets // 首页 81``` 82 83### 具体实现 84 85* zlib解压缩文件功能,包含添加文件,解压和压缩文件的接口,源码参考[ZipLibDecompressionPage.ets](entry/src/main/ets/zlib/ZipLibDecompressionPage.ets) 86 87 * 添加文件:通过调用fileio.openSync()创建文件并调用fileio.writeSync()向文件中写入内容。 88 * 压缩文件:通过调用zlib.zipFile()压缩文件。 89 * 解压文件:通过zlib.unzipFile解压文件。 90* 压缩多个文件功能,包含添加文件和压缩文件的接口,源码参考[CompressFilesPage.ets](entry/src/main/ets/zlib/CompressFilesPage.ets) 91 92 * 添加文件:通过调用fileio.openSync()创建文件并调用fileio.writeSync()向文件中写入内容。 93 * 压缩多个文件:通过调用zlib.compressFiles()压缩指定的多个文件。 94 * 计算压缩文件大小:通过调用getOriginalSize()计算.zip文件大小。 95* zlib压缩和解压数据相关的接口,源码参考[ZlibInterfaceListPage.ets](entry/src/main/ets/zlib/ZlibInterfaceListPage.ets) 96 97 * 创建zip对象:通过调用zlib.createZipSync()创建zip对象。 98 * 压缩数据初始化:通过调用zip.deflateInit()进行压缩数据的初始化。 99 100 * 校验数据:通过调用crc32()校验数据。 101 * 压缩数据:通过调用zip.deflate()压缩数据。 102 103 * 释放动态分配的数据结构:通过调用zip.deflateEnd()释放动态分配的数据结构。 104 * 创建zip对象:通过调用zlib.createZipSync()创建zip对象。 105 106 * 解压数据初始化:通过调用zip.inflateInit()进行解压数据的初始化。 107 108 * 校验数据:通过调用crc32()校验数据。 109 110 * 解压数据:通过调用zip.inflate()解压数据。 111 112 * 释放动态分配的数据结构:通过调用zip.inflateEnd()释放动态分配的数据结构。 113* gzip将数据压缩和解压相关的接口,源码参考[ZLibInterfaceListPage.ets](entry/src/main/ets/zlib/ZLibInterfaceListPage.ets) 114 115 * 创建gzip对象:通过调用zlib.createGZipSync()创建gzip对象。 116 * 打开文件:通过调用gzip.gzopen()打开文件并进行读写操作。 117 118 * 将数据压缩并写入文件:通过调用gzip.gzfwrite()将数据压缩并写入文件。 119 * 刷新文件:通过调用gzip.gzflush刷新文件。 120 121 * 关闭文件:通过调用gzip.gzclose关闭文件。 122 * 创建gzip对象:通过调用zlib.createGZipSync()创建gzip对象。 123 124 * 打开文件:通过调用gzip.gzopen()打开文件并进行读写操作。 125 126 * 从文件中读取和解压数据:通过调用gzip.gzfread()从文件中读取和解压数据。 127 128 * 关闭文件:通过调用gzip.gzclose关闭文件。 129 130### 相关权限 131 132不涉及。 133 134### 依赖 135 136不涉及。 137 138### 约束与限制 139 1401.本示例仅支持标准系统上运行,支持设备:RK3568。 141 1422.本示例需要使用DevEco Studio 4.1 Beta2 (Build Version: 4.1.0.400, built on April 9, 2024)才可编译运行。 143 1443.解压与压缩相关接口的入参路径必须为context相关接口获取的相对路径,目前该接口仅支持操作应用目录下的文件。 145 1464.本示例已适配API version 12版本SDK,版本号 5.0.0.28。 147 148### 下载 149 150如需单独下载本工程,执行如下命令: 151 152``` 153git init 154git config core.sparsecheckout true 155echo code/LaunguageBaseClassLibrary/ZipLib/ > .git/info/sparse-checkout 156git remote add origin https://gitee.com/openharmony/applications_app_samples.git 157git pull origin master 158```