• Home
Name Date Size #Lines LOC

..--

AppScope/22-Oct-2025-3633

entry/22-Oct-2025-2,5082,339

hvigor/22-Oct-2025-2422

screenshots/device/22-Oct-2025-

.gitignoreD22-Oct-2025121 1010

README.mdD22-Oct-20251.3 KiB3618

README_zh.mdD22-Oct-20257.7 KiB15693

build-profile.json5D22-Oct-20251.1 KiB4342

hvigorfile.tsD22-Oct-2025158 21

hvigorwD22-Oct-20252.1 KiB6228

hvigorw.batD22-Oct-20252 KiB7356

oh-package.json5D22-Oct-2025812 2725

ohosTest.mdD22-Oct-20251.6 KiB1513

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![](./screenshots/device/Screenshot_20231208091553194.jpeg)
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-basic-services-kit/js-apis-zlib.md)
6和[@ohos.fileio](https://gitee.com/openharmony/docs/blob/master/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"  /> | ![](./screenshots/device/Screenshot_20231208091638669.jpeg) | ![](./screenshots/device/Screenshot_20231208091619146.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
42
43
44gzip格式解压缩使用说明:
45
461.主界面点击**zLib相关接口调用**按钮,进入zLib相关接口调用界面。
47
482.zLib相关接口调用界面中点击**gzip格式解压缩**按钮,提示“gzip文件压缩成功,压缩数据为:hello,hello!",数据压入gzipTest.gz文件中,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/cache/。
49
503.等待约3s后,提示“gzip文件解压成功,解压数据为:hello,hello!",并将解压后的结果写入gzipTest.txt文件中,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/cache/。
51
52
53
54zlib格式解压缩使用说明:
55
561.主界面点击**zLib相关接口调用**按钮,进入zLib相关接口调用界面。
57
582.zLib相关接口调用界面中点击**zlib格式解压缩**按钮,提示“zlib文件压缩成功,压缩数据为: hello,hello!",并将压缩后的结果写入zlibDeflateFile.txt文件中,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/cache/。
59
603.等待约3s后,提示“zlib文件解压成功,解压数据为: hello,hello!",并将解压后的结果写入zlibInflateFile.txt文件中,同时文件的物理地址为/data/app/el2/100/base/ohos.samples.ziplib/haps/entry/cache/。
61
62
63### 工程目录
64
65```
66entry/src/main/ets/
67|---common
68|   |---AddDialog.ets                       // 弹窗组件
69|---entryability
70|---model
71|   |---DataSource.ets                      // 懒加载文件
72|   |---Logger.ts                           // 日志文件
73|---zlib
74|   |---CompressFilesPage.ets               // 压缩多个文件
75|   |---ZipLibDecompressionPage.ets         // ZipLib解压缩界面
76|   |---ZlibInterfaceListPage.ets           // 接口分类界面,包含zlib解压缩、gzip解压缩、crc校验功能
77|---pages
78|   |---Index.ets                           // 首页
79```
80
81### 具体实现
82
83* zlib解压缩文件功能,包含添加文件,解压和压缩文件的接口,源码参考[ZipLibDecompressionPage.ets](entry/src/main/ets/zlib/ZipLibDecompressionPage.ets)
84
85  * 添加文件:通过调用fileio.openSync()创建文件并调用fileio.writeSync()向文件中写入内容。
86  * 压缩文件:通过调用zlib.zipFile()压缩文件。
87  * 解压文件:通过zlib.unzipFile解压文件。
88* 压缩多个文件功能,包含添加文件和压缩文件的接口,源码参考[CompressFilesPage.ets](entry/src/main/ets/zlib/CompressFilesPage.ets)
89
90  * 添加文件:通过调用fileio.openSync()创建文件并调用fileio.writeSync()向文件中写入内容。
91  * 压缩多个文件:通过调用zlib.compressFiles()压缩指定的多个文件。
92  * 计算压缩文件大小:通过调用getOriginalSize()计算.zip文件大小。
93* zlib压缩和解压数据相关的接口,源码参考[ZlibInterfaceListPage.ets](entry/src/main/ets/zlib/ZlibInterfaceListPage.ets)
94
95  * 创建zip对象:通过调用zlib.createZipSync()创建zip对象。
96  * 压缩数据初始化:通过调用zip.deflateInit()进行压缩数据的初始化。
97
98  * 校验数据:通过调用crc32()校验数据。
99  * 压缩数据:通过调用zip.deflate()压缩数据。
100
101  * 释放动态分配的数据结构:通过调用zip.deflateEnd()释放动态分配的数据结构。
102  * 创建zip对象:通过调用zlib.createZipSync()创建zip对象。
103
104  * 解压数据初始化:通过调用zip.inflateInit()进行解压数据的初始化。
105
106  * 校验数据:通过调用crc32()校验数据。
107
108  * 解压数据:通过调用zip.inflate()解压数据。
109
110  * 释放动态分配的数据结构:通过调用zip.inflateEnd()释放动态分配的数据结构。
111* gzip将数据压缩和解压相关的接口,源码参考[ZLibInterfaceListPage.ets](entry/src/main/ets/zlib/ZLibInterfaceListPage.ets)
112
113  * 创建gzip对象:通过调用zlib.createGZipSync()创建gzip对象。
114  * 打开文件:通过调用gzip.gzopen()打开文件并进行读写操作。
115
116  * 将数据压缩并写入文件:通过调用gzip.gzfwrite()将数据压缩并写入文件。
117  * 刷新文件:通过调用gzip.gzflush刷新文件。
118
119  * 关闭文件:通过调用gzip.gzclose关闭文件。
120  * 创建gzip对象:通过调用zlib.createGZipSync()创建gzip对象。
121
122  * 打开文件:通过调用gzip.gzopen()打开文件并进行读写操作。
123
124  * 从文件中读取和解压数据:通过调用gzip.gzfread()从文件中读取和解压数据。
125
126  * 关闭文件:通过调用gzip.gzclose关闭文件。
127
128### 相关权限
129
130不涉及。
131
132### 依赖
133
134不涉及。
135
136### 约束与限制
137
1381.本示例仅支持标准系统上运行,支持设备:RK3568。
139
1402.本示例需要使用DevEco Studio 4.1 Beta2 (Build Version: 4.1.0.400, built on April 9, 2024)才可编译运行。
141
1423.解压与压缩相关接口的入参路径必须为context相关接口获取的相对路径,目前该接口仅支持操作应用目录下的文件。
143
1444.本示例已适配API version 12版本SDK,版本号 5.0.0.28。
145
146### 下载
147
148如需单独下载本工程,执行如下命令:
149
150```
151git init
152git config core.sparsecheckout true
153echo code/LanguageBaseClassLibrary/ZipLib/ > .git/info/sparse-checkout
154git remote add origin https://gitee.com/openharmony/applications_app_samples.git
155git pull origin master
156```