1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { common } from '@kit.AbilityKit'; 17import { hilog } from '@kit.PerformanceAnalysisKit'; 18import { preferences } from '@kit.ArkData'; 19import { promptAction, router } from '@kit.ArkUI'; 20import { zlib } from '@kit.BasicServicesKit'; 21import fileio from '@ohos.fileio'; 22import { AddDialog } from '../common/AddDialog'; 23import { ZipLibSource } from '../model/DataSource'; 24 25const DOMAIN = 0xF811; 26const BUNDLE = 'ZipLib_'; 27const TAG: string = '[CompressFilesPage]'; 28let fileList: preferences.Preferences; 29 30@Entry 31@Component 32struct CompressFilesPage { 33 @State isInserted: boolean = false; 34 @State files: ZipLibSource = new ZipLibSource([]); 35 @State fileName: string = ''; 36 @State slectFileName: string[] = []; 37 @State zipFileNames: string[] = []; 38 @State fileContent: string = ''; 39 @State isSelect: boolean = false; 40 private path: string = ''; 41 private title: Resource = $r('app.string.CompressFiles'); 42 private dialogController: CustomDialogController = new CustomDialogController({ 43 builder: AddDialog({ 44 fileName: this.fileName, 45 fileContent: this.fileContent, 46 isInserted: this.isInserted, 47 createFile: async (isInserted: boolean, fileName: string, fileContent: string) => { 48 hilog.info(DOMAIN, TAG, BUNDLE + `enter the createFile`); 49 this.fileName = `${fileName}.txt`; 50 let isDuplication = this.files.fileData.includes(this.fileName); 51 hilog.info(DOMAIN, TAG, BUNDLE + `isInserted = ${isInserted} isDuplication = ${isDuplication}`); 52 if (!isInserted || isDuplication) { 53 return; 54 } 55 let fd = fileio.openSync(`${this.path}/${this.fileName}`, 0o100 | 0o2, 0o666); 56 let number = fileio.writeSync(fd, fileContent); 57 hilog.info(DOMAIN, TAG, BUNDLE + `fd = ${fd} number = ${number}`); 58 this.files.pushData(this.fileName); 59 hilog.info(DOMAIN, TAG, BUNDLE + `this.files = ${JSON.stringify(this.files.fileData)}`); 60 await fileList.put('fileNames', JSON.stringify(this.files.fileData)); 61 await fileList.flush(); 62 } 63 }), 64 autoCancel: true, 65 }) 66 67 async aboutToAppear() { 68 fileList = await preferences.getPreferences(getContext(this), 'fileList'); 69 let ctx = getContext(this) as common.Context; 70 this.path = ctx.filesDir; 71 let value = await fileList.get('fileNames', ''); 72 this.files.fileData = JSON.parse(`${value}`); 73 this.files.notifyDataReload(); 74 this.zipFileNames.length = 0; 75 this.zipFileNames = this.files.fileData.filter(item => item.endsWith('.zip')); 76 } 77 78 async zipcompressFiles(path: string, zipfileNames: Array<string>): Promise<void> { 79 let files: string[] = []; 80 for (let index = 0; index < zipfileNames.length; index++) { 81 files[index] = `${path}/${zipfileNames[index]}`; 82 } 83 let zipFiles = Array.from(files); 84 hilog.debug(DOMAIN, TAG, BUNDLE + `zipFile = ${zipFiles}`); 85 let newName = `test_` + this.zipFileNames.length + `.zip`; 86 let zipOutFile = `${this.path}/${newName}`; 87 hilog.debug(DOMAIN, TAG, BUNDLE + `zipOutFile = ${zipOutFile}`); 88 89 if (this.files.fileData.includes(newName)) { 90 promptAction.showToast({ 91 message: $r('app.string.warning_failed'), 92 bottom: '11%' 93 }); 94 return; 95 } 96 try { 97 await zlib.compressFiles(zipFiles, zipOutFile, {}).then(data => { 98 zlib.getOriginalSize(zipOutFile).then((num) => { 99 promptAction.showToast({ 100 message: $r('app.string.CompressFiles_success_size', num), 101 bottom: '11%' 102 }); 103 }) 104 this.files.pushData(`${newName}`); 105 this.zipFileNames.push(newName); 106 }) 107 } catch { 108 promptAction.showToast({ 109 message: $r('app.string.CompressFiles_failure'), 110 bottom: '11%' 111 }); 112 } 113 await fileList.put('fileNames', JSON.stringify(this.files.fileData)); 114 await fileList.flush(); 115 } 116 117 build() { 118 Column() { 119 Column() { 120 Row() { 121 Row() { 122 Image($r('app.media.icon_back')) 123 .size({ height: '71%' }) 124 .objectFit(ImageFit.Contain) 125 .onClick(() => { 126 router.pushUrl({ 127 url: 'zlib/ZLibInterfaceListPage' 128 }); 129 }) 130 Text(this.title) 131 .fontSize(20) 132 .margin({ left: 8 }) 133 .fontWeight(FontWeight.Bold) 134 } 135 136 Row() { 137 Image($r('app.media.add')) 138 .size({ height: '71%' }) 139 .id('addFile') 140 .margin({ left: '3%' }) 141 .objectFit(ImageFit.Contain) 142 .onClick(() => { 143 this.dialogController.open(); 144 }) 145 } 146 } 147 .justifyContent(FlexAlign.SpaceBetween) 148 .width('91%') 149 .height('9.75%') 150 .backgroundColor($r('app.color.backGrounding')) 151 152 List({ initialIndex: 0 }) { 153 LazyForEach(this.files, (item: string, index: number) => { 154 ListItem() { 155 Row() { 156 Row() { 157 Image(item.includes('.zip') ? $r('app.media.zip') : $r('app.media.file')) 158 .size({ height: '66.7%' }) 159 .objectFit(ImageFit.Contain) 160 Column() { 161 Text(item) 162 .width('50%') 163 .fontSize(16) 164 .margin({ left: 16 }) 165 } 166 } 167 168 Checkbox() 169 .visibility(item.includes('.zip') ? Visibility.Hidden : Visibility.Visible) 170 .width('6%') 171 .select(this.slectFileName.indexOf(item) != -1 ? true : false) 172 .id('file_' + index) 173 .selectedColor($r('app.color.checkbox_color')) 174 .shape(CheckBoxShape.CIRCLE) 175 .onChange((value: boolean) => { 176 if (value) { 177 this.isSelect = true; 178 this.slectFileName.push(item); 179 } else { 180 this.isSelect = false; 181 let index = this.slectFileName.indexOf(item); 182 if (index !== -1) { 183 this.slectFileName.splice(index, 1); 184 } 185 } 186 }) 187 } 188 .justifyContent(FlexAlign.SpaceBetween) 189 .padding({ right: 12, left: 12 }) 190 .width('100%') 191 .height('14.2%') 192 .borderRadius(16) 193 .backgroundColor($r('app.color.start_window_background')) 194 } 195 .margin({ bottom: 12 }) 196 }, (item: string) => item) 197 } 198 .margin({ top: 8 }) 199 .height('85.875%') 200 .width('91%') 201 .backgroundColor($r('app.color.index_bg')) 202 } 203 .height('80%') 204 .width('100%') 205 206 207 Column() { 208 Text($r('app.string.tip_CompressFiles')) 209 .textAlign(TextAlign.Start) 210 .fontSize(12) 211 .fontColor($r('app.color.tip_Gzip_backeGround')) 212 .width('86.4%') 213 .height('22.3%') 214 .margin({ bottom: 24 }) 215 216 Row() { 217 Button($r('app.string.zip')) 218 .id('compress') 219 .height('100%') 220 .width('100%') 221 .fontSize(16) 222 .fontFamily('HarmonyHeiTi') 223 .type(ButtonType.Capsule) 224 .backgroundColor($r('app.color.button_color')) 225 .fontColor($r('app.color.text_white_color')) 226 .borderRadius(20) 227 .onClick(() => { 228 if (this.isSelect) { 229 this.zipcompressFiles(this.path, this.slectFileName); 230 } 231 }) 232 } 233 .margin({ bottom: 16 }) 234 .height('28%') 235 .width('91%') 236 } 237 .justifyContent(FlexAlign.End) 238 .alignItems(HorizontalAlign.Center) 239 .height('20%') 240 .width('100%') 241 } 242 .height('100%') 243 .width('100%') 244 .backgroundColor($r('app.color.index_bg')) 245 } 246} 247