Revision: 28313
          http://sourceforge.net/p/bibdesk/svn/28313
Author:   hofman
Date:     2023-07-14 14:49:10 +0000 (Fri, 14 Jul 2023)
Log Message:
-----------
create tar archive directly from linked files and .bib file, no need to copy 
them to a temporary folder

Modified Paths:
--------------
    trunk/bibdesk/BibDocument.m

Modified: trunk/bibdesk/BibDocument.m
===================================================================
--- trunk/bibdesk/BibDocument.m 2023-07-14 09:28:42 UTC (rev 28312)
+++ trunk/bibdesk/BibDocument.m 2023-07-14 14:49:10 UTC (rev 28313)
@@ -1258,8 +1258,7 @@
 
 - (BOOL)writeArchiveToURL:(NSURL *)fileURL error:(NSError **)outError{
     NSURL *dirURL = [fileURL URLByDeletingLastPathComponent];
-    NSString *targetName = [[saveTargetURL lastPathComponent] 
stringByDeletingPathExtension];
-    NSURL *folderURL = [dirURL URLByAppendingPathComponent:targetName 
isDirectory:YES];
+    NSString *bibtexFile = [[[saveTargetURL lastPathComponent] 
stringByDeletingPathExtension] stringByAppendingPathExtension:@"bib"];
     NSFileManager *fm = [NSFileManager defaultManager];
     NSWorkspace *ws = [NSWorkspace sharedWorkspace];
     NSString *filePath;
@@ -1267,56 +1266,43 @@
     NSString *commonParent = nil;
     NSMutableSet *localFiles = [NSMutableSet set];
     NSSet *localFileFields = [[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKUseLocalUrlAndUrlKey] ? [[BDSKTypeManager sharedManager] 
localFileFieldsSet] : nil;
-    BOOL success = [fm createDirectoryAtURL:folderURL 
withIntermediateDirectories:NO attributes:nil error:NULL];
+    BOOL success = YES;
     
-    if (success) {
-        for (BibItem *item in [self publicationsForSaving]) {
-            for (BDSKLinkedFile *file in [item localFiles]) {
-                if ((filePath = [file path]) && [fm fileExistsAtPath:filePath] 
&& [ws isFolderAtPath:filePath] == NO) {
-                    [localFiles addObject:filePath];
-                    [parents addObject:[filePath 
stringByDeletingLastPathComponent]];
-                }
+    for (BibItem *item in [self publicationsForSaving]) {
+        for (BDSKLinkedFile *file in [item localFiles]) {
+            if ((filePath = [file path]) && [fm fileExistsAtPath:filePath] && 
[ws isFolderAtPath:filePath] == NO) {
+                [localFiles addObject:filePath];
+                [parents addObject:[filePath 
stringByDeletingLastPathComponent]];
             }
-            for (NSString *field in localFileFields) {
-                if ((filePath = [[item localFileURLForField:field] path]) && 
[fm fileExistsAtPath:filePath] && [ws isFolderAtPath:filePath] == NO) {
-                    [localFiles addObject:filePath];
-                    [parents addObject:[filePath 
stringByDeletingLastPathComponent]];
-                }
+        }
+        for (NSString *field in localFileFields) {
+            if ((filePath = [[item localFileURLForField:field] path]) && [fm 
fileExistsAtPath:filePath] && [ws isFolderAtPath:filePath] == NO) {
+                [localFiles addObject:filePath];
+                [parents addObject:[filePath 
stringByDeletingLastPathComponent]];
             }
         }
-        
-        for (filePath in parents)
-            commonParent = commonParent ? [filePath 
commonRootPathOfFile:commonParent] : filePath;
-        
-        NSData *bibtexData = [self bibTeXDataDroppingInternal:NO 
relativeToPath:commonParent error:outError];
-        NSURL *bibtexURL = [folderURL URLByAppendingPathComponent:[targetName 
stringByAppendingPathExtension:@"bib"] isDirectory:NO];
+    }
+    
+    for (filePath in parents)
+        commonParent = commonParent ? [filePath 
commonRootPathOfFile:commonParent] : filePath;
+    
+    NSData *bibtexData = [self bibTeXDataDroppingInternal:NO 
relativeToPath:commonParent error:outError];
+    NSURL *bibtexURL = [dirURL URLByAppendingPathComponent:bibtexFile 
isDirectory:NO];
 
-        success = [bibtexData writeToURL:bibtexURL options:0 error:outError];
-        
-        for (filePath in localFiles) {
-            if (success == NO) break;
-            NSString *relativePath = commonParent ? [filePath 
relativePathFromPath:commonParent] : [filePath lastPathComponent];
-            NSURL *targetURL = [folderURL 
URLByAppendingPathComponent:relativePath isDirectory:NO];
-            
-            if ([targetURL checkResourceIsReachableAndReturnError:NULL])
-                targetURL = [fm uniqueFileURL:targetURL];
-            success = [fm createPathToURL:targetURL attributes:nil];
-            if (success)
-                success = [fm copyItemAtURL:[NSURL fileURLWithPath:filePath 
isDirectory:NO] toURL:targetURL error:NULL];
-        }
-        
-        if (success) {
-            NSTask *task = [[[BDSKTask alloc] init] autorelease];
-            [task setLaunchPath:@"/usr/bin/tar"];
-            [task setArguments:[NSArray arrayWithObjects:@"-czf", [[fileURL 
path] lastPathComponent], targetName, nil]];
-            [task setCurrentDirectoryPath:[dirURL path]];
-            @try { [task launch]; }
-            @catch (id e) {}
-            [task waitUntilExit];
-            success = [task terminationStatus] == 0;
-        }
-        
-        [fm removeItemAtURL:folderURL error:NULL];
+    success = [bibtexData writeToURL:bibtexURL options:0 error:outError];
+    
+    if (success) {
+        NSMutableArray *args = [NSMutableArray arrayWithObjects:@"-czf", 
[fileURL path], bibtexFile, @"-C", commonParent, nil];
+        for (filePath in localFiles)
+            [args addObject:[filePath relativePathFromPath:commonParent]];
+        NSTask *task = [[[BDSKTask alloc] init] autorelease];
+        [task setLaunchPath:@"/usr/bin/tar"];
+        [task setArguments:args];
+        [task setCurrentDirectoryPath:[dirURL path]];
+        @try { [task launch]; }
+        @catch (id e) {}
+        [task waitUntilExit];
+        success = [task terminationStatus] == 0;
     }
     
     return success;

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to