Browse Source

【功能】拆分ZIP

xingcheng02 1 year ago
parent
commit
c78d7b9e69
2 changed files with 29 additions and 9 deletions
  1. 1 0
      XCTheme.podspec
  2. 28 9
      XCTheme/Classes/Impl/XCThemeService.m

+ 1 - 0
XCTheme.podspec

@@ -33,6 +33,7 @@ A Theme Manager for C Code Develop & C Notebook.
   s.source_files = 'XCTheme/Classes/**/*'
   s.source_files = 'XCTheme/Classes/**/*'
   
   
   s.dependency 'XCModuleLoader'
   s.dependency 'XCModuleLoader'
+  s.dependency 'XCZip'
   
   
   # s.resource_bundles = {
   # s.resource_bundles = {
   #   'XCTheme' => ['XCTheme/Assets/*.png']
   #   'XCTheme' => ['XCTheme/Assets/*.png']

+ 28 - 9
XCTheme/Classes/Impl/XCThemeService.m

@@ -7,11 +7,7 @@
 
 
 #import "XCThemeProtocol.h"
 #import "XCThemeProtocol.h"
 #import "XCTheme-Swift.h"
 #import "XCTheme-Swift.h"
-
-static dispatch_queue_t globalThemeDownloadQueue;
-__attribute__((constructor)) static void init(void) {
-    globalThemeDownloadQueue = dispatch_queue_create("org.forgetive.notebook.themeDownload", DISPATCH_QUEUE_SERIAL);
-}
+#import <XCZip/XCZip.h>
 
 
 @xcmlservice(XCThemeProtocol, XCThemeService)
 @xcmlservice(XCThemeProtocol, XCThemeService)
 @synthesize downloadBlock = _downloadBlock;
 @synthesize downloadBlock = _downloadBlock;
@@ -88,14 +84,37 @@ __attribute__((constructor)) static void init(void) {
     }
     }
     
     
     if (!self.downloadBlock) {
     if (!self.downloadBlock) {
-        completion(NO, @"downloadBlock not specified", nil);
+        if (completion) {
+            completion(NO, @"downloadBlock not specified", nil);
+        }
         @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"downloadBlock not specified" userInfo:nil];
         @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"downloadBlock not specified" userInfo:nil];
     }
     }
     
     
-    NSString *downloadTHMPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
-    downloadTHMPath = [downloadTHMPath stringByAppendingFormat:@"/ThemeResources/%@.thm", themeId];
+    NSString *rootPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
+    NSString *downloadTHMPath = [rootPath stringByAppendingFormat:@"/ThemeResources/%@.thm", themeId];
+    NSString *extractPath = [rootPath stringByAppendingFormat:@"/ThemeResources/%@", themeId];
     self.downloadBlock(themeId, downloadTHMPath, ^(BOOL success, NSString * _Nonnull reason) {
     self.downloadBlock(themeId, downloadTHMPath, ^(BOOL success, NSString * _Nonnull reason) {
-        
+        BOOL extractSuccess = [SSZipArchive unzipFileAtPath:downloadTHMPath toDestination:extractPath uniqueId:nil];
+        [NSFileManager.defaultManager removeItemAtPath:downloadTHMPath error:nil];
+        if (!extractSuccess) {
+            [NSFileManager.defaultManager removeItemAtPath:extractPath error:nil];
+            if (completion) {
+                completion(NO, @"Extract download failed", nil);
+            }
+            return;
+        }
+        XCThemeSpec *spec = [[XCThemeSpec alloc] initWithThemeId:themeId];
+        if (spec) {
+            XCThemeSpecModel *specModel = [[XCThemeSpecModel alloc] initWithWrappedObject:spec];
+            if (completion) {
+                completion(YES, @"", specModel);
+            }
+        } else {
+            [NSFileManager.defaultManager removeItemAtPath:extractPath error:nil];
+            if (completion) {
+                completion(NO, @"Theme not completed", nil);
+            }
+        }
     });
     });
 }
 }