|
|
AIWROKÈí¼þZip ¸ß¼¶²Ù×÷¸´ÔÓʾÀý
- // AIWROK Èí¼þ - Zip ¸ß¼¶²Ù×÷¸´ÔÓʾÀý
- // ×÷Õß: AIWROK ¿ª·¢ÍŶÓ
- // ÈÕÆÚ: 2024Äê6ÔÂ
- // ˵Ã÷: ±¾Ê¾ÀýÑÝʾÁËzip¶ÔÏóµÄ¸´ÔÓÓ÷¨£¬°üÀ¨ÏÂÔØ¡¢Ñ¹Ëõ¡¢½âѹ¡¢½ø¶ÈÏÔʾ¡¢´íÎó´¦ÀíµÈ
- // ÔËÐл·¾³: AIWROK Èí¼þ Android °æ±¾
- printl("===== AIWROK Zip ¸ß¼¶²Ù×÷¸´ÔÓʾÀý¿ªÊ¼ =====");
- // ÅäÖòÎÊý
- var config = {
- // ÁÙʱĿ¼
- tempDir: '/sdcard/aiwrok_zip_test',
- // ÏÂÔØÎļþĿ¼
- downloadDir: '/sdcard/aiwrok_zip_test/downloads',
- // ѹËõÎļþĿ¼
- zipDir: '/sdcard/aiwrok_zip_test/zips',
- // ½âѹÎļþĿ¼
- unzipDir: '/sdcard/aiwrok_zip_test/unzipped',
- // ÒªÏÂÔØµÄÎļþÁбí
- downloadFiles: [
- { name: 'test1.txt', content: 'ÕâÊDzâÊÔÎļþ1µÄÄÚÈÝ£¬ÓÃÓÚÑÝʾzip²Ù×÷¡£' },
- { name: 'test2.txt', content: 'ÕâÊDzâÊÔÎļþ2µÄÄÚÈÝ£¬ÓÃÓÚÑÝʾzip²Ù×÷µÄ¸ß¼¶¹¦ÄÜ¡£' },
- { name: 'test3.txt', content: 'ÕâÊDzâÊÔÎļþ3µÄÄÚÈÝ£¬°üº¬Ò»Ð©ÌØÊâ×Ö·û£ºÖÐÎÄ¡¢Ó¢ÎÄ¡¢Êý×Ö1234567890' }
- ],
- // ²âÊÔͼƬbase64£¨¼ò»¯°æ£©
- testImageBase64: 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='
- };
- // 1. ×¼±¸¹¤×÷£º´´½¨²âÊÔĿ¼
- printl("\n1. ×¼±¸¹¤×÷£º´´½¨²âÊÔĿ¼");
- function prepareDirectories() {
- try {
- // ´´½¨Ä¿Â¼½á¹¹
- var dirs = [config.tempDir, config.downloadDir, config.zipDir, config.unzipDir];
- for (var i = 0; i < dirs.length; i++) {
- if (!new java.io.File(dirs[i]).exists()) {
- file.createFloder(dirs[i]);
- printl("✓ ´´½¨Ä¿Â¼³É¹¦: " + dirs[i]);
- } else {
- printl("⚠️ Ŀ¼ÒÑ´æÔÚ: " + dirs[i]);
- }
- }
- return true;
- } catch (e) {
- printl("✗ ´´½¨Ä¿Â¼Ê§°Ü: " + e.message);
- return false;
- }
- }
- // 2. ´´½¨²âÊÔÎļþ
- printl("\n2. ´´½¨²âÊÔÎļþ");
- function createTestFiles() {
- try {
- // ´´½¨Îı¾Îļþ
- for (var i = 0; i < config.downloadFiles.length; i++) {
- var filePath = config.downloadDir + '/' + config.downloadFiles[i].name;
- // ÏÈ´´½¨Îļþ
- file.createFloder(filePath);
- // È»ºóдÈëÄÚÈÝ
- try {
- // ³¢ÊÔʹÓò»Í¬µÄÎļþдÈë·½·¨
- var writeSuccess = false;
-
- if (typeof Îļþ !== 'undefined' && typeof Îļþ.ɾ³ýд === 'function') {
- // ʹÓÃÎļþ.ɾ³ýд·½·¨
- writeSuccess = Îļþ.ɾ³ýд(filePath, config.downloadFiles[i].content);
- } else if (typeof file !== 'undefined' && typeof file.write === 'function') {
- // ʹÓÃfile.write·½·¨
- writeSuccess = file.write(filePath, config.downloadFiles[i].content);
- } else {
- // Ö±½ÓʹÓÃJava´úÂëдÈëÎļþ
- var javaFile = new java.io.File(filePath);
- var writer = new java.io.BufferedWriter(new java.io.FileWriter(javaFile));
- writer.write(config.downloadFiles[i].content);
- writer.close();
- writeSuccess = true;
- }
-
- if (writeSuccess) {
- printl("✓ ´´½¨Îı¾Îļþ³É¹¦: " + config.downloadFiles[i].name);
- } else {
- printl("✗ ´´½¨Îı¾Îļþʧ°Ü: " + config.downloadFiles[i].name);
- }
- } catch (e) {
- printl("✗ ´´½¨Îı¾ÎļþÒì³£: " + config.downloadFiles[i].name + " - " + e.message);
- }
- }
-
- // ´´½¨Í¼Æ¬Îļþ
- var imagePath = config.downloadDir + '/test_image.png';
- if (file.base64ToFile(config.testImageBase64, imagePath)) {
- printl("✓ ´´½¨Í¼Æ¬Îļþ³É¹¦: test_image.png");
- } else {
- printl("✗ ´´½¨Í¼Æ¬Îļþʧ°Ü");
- }
-
- return true;
- } catch (e) {
- printl("✗ ´´½¨²âÊÔÎļþʧ°Ü: " + e.message);
- return false;
- }
- }
- // 3. ½ø¶ÈÏÔʾº¯Êý
- function showProgress(message, current, total) {
- var percent = Math.round((current / total) * 100);
- printl(message + " " + percent + "% (" + current + "/" + total + ")");
- }
- // 4. ѹËõ²Ù×÷ʾÀý
- printl("\n3. ѹËõ²Ù×÷ʾÀý");
- function zipOperations() {
- try {
- // ʾÀý1: ѹËõÕû¸öÎļþ¼Ð
- printl("\n3.1 ѹËõÕû¸öÎļþ¼Ð");
- var folderZipPath = config.zipDir + '/entire_folder.zip';
- if (zip.zipFolder(config.downloadDir, folderZipPath)) {
- printl("✓ ѹËõÕû¸öÎļþ¼Ð³É¹¦: " + folderZipPath);
- printl(" Îļþ´óС: " + file.getSize(folderZipPath) + " ×Ö½Ú");
- } else {
- printl("✗ ѹËõÕû¸öÎļþ¼Ðʧ°Ü");
- }
-
- // ʾÀý2: ´´½¨¶à¸ö×ÓÎļþ¼Ð²¢Ñ¹ËõÌØ¶¨Îļþ
- printl("\n3.2 ´´½¨¶à×ÓÎļþ¼Ð²¢Ñ¹Ëõ");
- var subdir1 = config.tempDir + '/subdir1';
- var subdir2 = config.tempDir + '/subdir2';
-
- // ´´½¨×ÓÎļþ¼Ð
- file.createFloder(subdir1);
- file.createFloder(subdir2);
-
- // ¸´ÖÆÎļþµ½×ÓÎļþ¼Ð
- file.copy(config.downloadDir + '/test1.txt', subdir1 + '/test1.txt');
- file.copy(config.downloadDir + '/test2.txt', subdir2 + '/test2.txt');
- file.copy(config.downloadDir + '/test_image.png', subdir1 + '/test_image.png');
-
- // ѹËõÕû¸öÁÙʱĿ¼£¨°üº¬×ÓÎļþ¼Ð£©
- var multiZipPath = config.zipDir + '/multi_folder.zip';
- if (zip.zipFolder(config.tempDir, multiZipPath)) {
- printl("✓ ѹËõ¶à×ÓÎļþ¼Ð³É¹¦: " + multiZipPath);
- printl(" Îļþ´óС: " + file.getSize(multiZipPath) + " ×Ö½Ú");
- } else {
- printl("✗ ѹËõ¶à×ÓÎļþ¼Ðʧ°Ü");
- }
-
- return true;
- } catch (e) {
- printl("✗ ѹËõ²Ù×÷ʧ°Ü: " + e.message);
- printl(" ´íÎóÏêÇé: " + e.toString());
- return false;
- }
- }
- // 5. ½âѹ²Ù×÷ʾÀý
- printl("\n4. ½âѹ²Ù×÷ʾÀý");
- function unzipOperations() {
- try {
- // ʾÀý1: ½âѹÕû¸özipÎļþ
- printl("\n4.1 ½âѹÕû¸özipÎļþ");
- var zipPath = config.zipDir + '/entire_folder.zip';
- var targetUnzipDir = config.unzipDir + '/entire_unzip';
-
- // ´´½¨Ä¿±êĿ¼
- file.createFloder(targetUnzipDir);
-
- if (file.unZipFile(zipPath, targetUnzipDir)) {
- printl("✓ ½âѹÕû¸özipÎļþ³É¹¦: " + zipPath);
-
- // Áгö½âѹºóµÄÎļþ
- var files = file.getFilesAllName(targetUnzipDir);
- if (files && files.length > 0) {
- printl(" ½âѹºóµÄÎļþ:");
- for (var i = 0; i < files.length; i++) {
- printl(" " + (i + 1) + ". " + files[i]);
- }
- }
- } else {
- printl("✗ ½âѹÕû¸özipÎļþʧ°Ü");
- }
-
- // ʾÀý2: ½âѹ¶à¸özipÎļþµ½²»Í¬Ä¿Â¼
- printl("\n4.2 ÅúÁ¿½âѹ¶à¸özipÎļþ");
- var zipFiles = file.getFilesAllName(config.zipDir);
- if (zipFiles && zipFiles.length > 0) {
- for (var i = 0; i < zipFiles.length; i++) {
- var zipFileName = zipFiles[i];
- if (zipFileName.endsWith('.zip')) {
- var zipFilePath = config.zipDir + '/' + zipFileName;
- var targetDir = config.unzipDir + '/batch_unzip_' + i;
-
- // ´´½¨Ä¿±êĿ¼
- file.createFloder(targetDir);
-
- showProgress("ÕýÔÚ½âѹ " + zipFileName + "...", i + 1, zipFiles.length);
-
- if (file.unZipFile(zipFilePath, targetDir)) {
- printl("✓ ÅúÁ¿½âѹ³É¹¦: " + zipFileName);
- } else {
- printl("✗ ÅúÁ¿½âѹʧ°Ü: " + zipFileName);
- }
- }
- }
- }
-
- return true;
- } catch (e) {
- printl("✗ ½âѹ²Ù×÷ʧ°Ü: " + e.message);
- printl(" ´íÎóÏêÇé: " + e.toString());
- return false;
- }
- }
- // 6. ¸ß¼¶¹¦ÄÜ£ºÑ¹ËõÌØ¶¨ÀàÐ͵ÄÎļþ
- printl("\n5. ¸ß¼¶¹¦ÄÜ£ºÑ¹ËõÌØ¶¨ÀàÐ͵ÄÎļþ");
- function zipSpecificFiles() {
- try {
- // ´´½¨°üº¬²»Í¬ÀàÐÍÎļþµÄĿ¼
- var mixedDir = config.tempDir + '/mixed_files';
- file.createFloder(mixedDir);
-
- // ´´½¨²»Í¬ÀàÐ͵ÄÎļþ
- var testFiles = [
- { path: mixedDir + '/text1.txt', content: 'Îı¾Îļþ1' },
- { path: mixedDir + '/text2.txt', content: 'Îı¾Îļþ2' },
- { path: mixedDir + '/data.csv', content: 'ÁÐ1,ÁÐ2,ÁÐ3\nÖµ1,Öµ2,Öµ3\nÖµ4,Öµ5,Öµ6' }
- ];
-
- for (var i = 0; i < testFiles.length; i++) {
- var filePath = testFiles[i].path;
- var content = testFiles[i].content;
-
- // ÏÈ´´½¨Îļþ
- file.createFloder(filePath);
-
- // È»ºóдÈëÄÚÈÝ
- try {
- var writeSuccess = false;
-
- if (typeof Îļþ !== 'undefined' && typeof Îļþ.ɾ³ýд === 'function') {
- writeSuccess = Îļþ.ɾ³ýд(filePath, content);
- } else if (typeof file !== 'undefined' && typeof file.write === 'function') {
- writeSuccess = file.write(filePath, content);
- } else {
- var javaFile = new java.io.File(filePath);
- var writer = new java.io.BufferedWriter(new java.io.FileWriter(javaFile));
- writer.write(content);
- writer.close();
- writeSuccess = true;
- }
-
- if (writeSuccess) {
- printl("✓ ´´½¨Îļþ³É¹¦: " + filePath);
- } else {
- printl("✗ ´´½¨Îļþʧ°Ü: " + filePath);
- }
- } catch (e) {
- printl("✗ ´´½¨ÎļþÒì³£: " + filePath + " - " + e.message);
- }
- }
-
- // ½öѹËõtxtÎļþ
- printl("\n5.1 ½öѹËõtxtÎļþ");
- var txtZipPath = config.zipDir + '/only_txt_files.zip';
-
- // »ñÈ¡ËùÓÐtxtÎļþ
- var txtFiles = file.getTypeFilesName(mixedDir, '.txt');
- if (txtFiles && txtFiles.length > 0) {
- // ´´½¨ÁÙʱĿ¼´æ·ÅҪѹËõµÄtxtÎļþ
- var txtTempDir = config.tempDir + '/txt_only_temp';
- file.createFloder(txtTempDir);
-
- // ¸´ÖÆtxtÎļþµ½ÁÙʱĿ¼
- for (var i = 0; i < txtFiles.length; i++) {
- file.copy(mixedDir + '/' + txtFiles[i], txtTempDir + '/' + txtFiles[i]);
- }
-
- // ѹËõÁÙʱĿ¼
- if (zip.zipFolder(txtTempDir, txtZipPath)) {
- printl("✓ ½öѹËõtxtÎļþ³É¹¦: " + txtZipPath);
- printl(" ѹËõÁË " + txtFiles.length + " ¸ötxtÎļþ");
- } else {
- printl("✗ ½öѹËõtxtÎļþʧ°Ü");
- }
-
- // ɾ³ýÁÙʱĿ¼
- file.deleteFilesOfDir(txtTempDir);
- }
-
- return true;
- } catch (e) {
- printl("✗ ѹËõÌØ¶¨ÀàÐÍÎļþʧ°Ü: " + e.message);
- return false;
- }
- }
- // 7. ´íÎó´¦ÀíʾÀý
- printl("\n6. ´íÎó´¦ÀíʾÀý");
- function errorHandlingExamples() {
- try {
- printl("\n6.1 ²âÊÔ²»´æÔÚµÄÎļþ½âѹ");
- var nonExistentZip = config.zipDir + '/non_existent.zip';
- var targetDir = config.unzipDir + '/error_test';
- file.createFloder(targetDir);
-
- try {
- if (file.unZipFile(nonExistentZip, targetDir)) {
- printl("✓ ½âѹ²»´æÔÚµÄÎļþ³É¹¦£¨ÕâÊÇÒ»¸ö´íÎó£©");
- } else {
- printl("✓ ÕýÈ·´¦ÀíÁ˲»´æÔÚµÄÎļþ½âѹÇëÇó");
- }
- } catch (e) {
- printl("✓ ÕýÈ·²¶»ñÁ˽âѹ²»´æÔÚÎļþµÄÒì³£: " + e.message);
- }
-
- printl("\n6.2 ²âÊÔѹËõ¿ÕĿ¼");
- var emptyDir = config.tempDir + '/empty_dir';
- file.createFloder(emptyDir);
- var emptyZipPath = config.zipDir + '/empty.zip';
-
- try {
- if (zip.zipFolder(emptyDir, emptyZipPath)) {
- printl("✓ ѹËõ¿ÕĿ¼³É¹¦");
- printl(" ¿ÕzipÎļþ´óС: " + file.getSize(emptyZipPath) + " ×Ö½Ú");
- } else {
- printl("✗ ѹËõ¿ÕĿ¼ʧ°Ü");
- }
- } catch (e) {
- printl("✓ ÕýÈ·²¶»ñÁËѹËõ¿ÕĿ¼µÄÒì³£: " + e.message);
- }
-
- return true;
- } catch (e) {
- printl("✗ ´íÎó´¦ÀíʾÀýʧ°Ü: " + e.message);
- return false;
- }
- }
- // 8. ÇåÀí¹¤×÷
- printl("\n7. ÇåÀí¹¤×÷");
- function cleanUp() {
- try {
- // ɾ³ýËùÓвâÊÔĿ¼ºÍÎļþ
- if (file.deleteFilesOfDir(config.tempDir)) {
- printl("✓ ³É¹¦ÇåÀíËùÓвâÊÔÎļþºÍĿ¼: " + config.tempDir);
- } else {
- printl("✗ ÇåÀí²âÊÔÎļþºÍĿ¼ʧ°Ü£¬¿ÉÄÜÐèÒªÊÖ¶¯ÇåÀí");
- }
- return true;
- } catch (e) {
- printl("✗ ÇåÀí¹¤×÷ʧ°Ü: " + e.message);
- printl("Ìáʾ: ÇëÊÖ¶¯ÇåÀíÊ£ÓàÎļþºÍĿ¼: " + config.tempDir);
- return false;
- }
- }
- // Ö´ÐÐÁ÷³Ì
- try {
- // 1. ×¼±¸Ä¿Â¼
- if (!prepareDirectories()) {
- throw new Error("×¼±¸Ä¿Â¼Ê§°Ü");
- }
-
- // 2. ´´½¨²âÊÔÎļþ
- if (!createTestFiles()) {
- throw new Error("´´½¨²âÊÔÎļþʧ°Ü");
- }
-
- // 3. Ö´ÐÐѹËõ²Ù×÷
- if (!zipOperations()) {
- throw new Error("ѹËõ²Ù×÷ʧ°Ü");
- }
-
- // 4. Ö´Ðнâѹ²Ù×÷
- if (!unzipOperations()) {
- throw new Error("½âѹ²Ù×÷ʧ°Ü");
- }
-
- // 5. Ö´ÐÐÌØ¶¨ÎļþѹËõ
- if (!zipSpecificFiles()) {
- throw new Error("ѹËõÌØ¶¨ÀàÐÍÎļþʧ°Ü");
- }
-
- // 6. Ö´ÐдíÎó´¦ÀíʾÀý
- if (!errorHandlingExamples()) {
- throw new Error("´íÎó´¦ÀíʾÀýʧ°Ü");
- }
-
- // 7. ÇåÀí¹¤×÷
- if (!cleanUp()) {
- throw new Error("ÇåÀí¹¤×÷ʧ°Ü");
- }
-
- printl("\n🎉 ËùÓвÙ×÷Ö´Ðгɹ¦£¡");
-
- } catch (e) {
- printl("\n❌ Ö´Ðйý³ÌÖз¢Éú´íÎó: " + e.message);
- printl(" ´íÎóÏêÇé: " + e.toString());
- printl("Ìáʾ: Çë¼ì²é´íÎóÐÅÏ¢²¢ÊÖ¶¯ÇåÀí¿ÉÄܵIJÐÁôÎļþ");
- }
- printl("\n===== AIWROK Zip ¸ß¼¶²Ù×÷¸´ÔÓʾÀý½áÊø =====");
- printl("\n×¢ÒâÊÂÏî:");
- printl("1. ËùÓвÙ×÷¶¼ÐèÒªÊʵ±µÄÎļþϵͳȨÏÞ");
- printl("2. ´óÐÍÎļþµÄѹËõºÍ½âѹ¿ÉÄÜÐèÒª½Ï³¤Ê±¼ä");
- printl("3. ½¨ÒéÔÚʵ¼ÊÓ¦ÓÃÖÐÌí¼Ó¸ü¶àµÄ´íÎó´¦ÀíºÍ½ø¶ÈÏÔʾ");
- printl("4. ¿ÉÒÔ¸ù¾ÝÐèÒªÐÞ¸ÄÅäÖòÎÊýÒÔÊÊÓ¦²»Í¬µÄÓ¦Óó¡¾°");
- printl("5. ±¾Ê¾ÀýÑÝʾÁËzip.unZipFileºÍzip.zipFolderµÄ¸ß¼¶Ó÷¨");
¸´ÖÆ´úÂë
|
|