0

I was wondering if there is a method to zip files without using third party libraries and using only stuff from .

I am attempting to use NSData CompressDataUsingAlgorithm:NSDataCompressionAlgorithmZlib to compress the data then using the NSData writeToURL:@"Test.zip".

This method creates a compressed file that is the proper size but unable to be read/archived by any sources. I know this since I use the archive function in the finder and get a zip archive that can be archived.

So I have a feeling I am doing this incorrectly, so is there any other methods to zip folders? or is there another step before writing to file that I am missing?

Thanks

Edit: Added my code

NSFileWrapper *dirWrapper = [[NSFileWrapper alloc] initWithURL:url options:0 error:&tep];
NSData *dirData = [dirWrapper serializedRepresentation];
NSData *gzData = [dirData compressedDataUsingAlgorithm:NSDataCompressionAlgorithmZlib error:&tep];
[gzData writeToURL:Url2 atomically:YES];
dkovacev
  • 33
  • 4
  • I am not presenting code, I am presenting class function names. I dont understand why you would reply with such a comment. If writing all of my code in a snippet would help answer it I would be happy to do so. But you dont have to be rude about this. – dkovacev Aug 16 '21 at 06:30
  • Does this answer your question? [How to compress a file without using any external libraries?](https://stackoverflow.com/questions/8431283/how-to-compress-a-file-without-using-any-external-libraries) – cansyrup756 Aug 16 '21 at 06:55
  • Not really cansyrup, I came across this question previously and have tried an implementation of libz and ended up getting within the archive just a file containing the data itself and not the actual files since I have that filewrapper component involved. Because MacOS has the compress function in finder, so there must be some method. – dkovacev Aug 16 '21 at 07:20
  • Does this answer your question? [What does a zlib header look like?](https://stackoverflow.com/questions/9050260/what-does-a-zlib-header-look-like) – Willeke Aug 16 '21 at 10:30
  • Thanks Willeke, this will probably help me out. – dkovacev Aug 16 '21 at 19:30

1 Answers1

1

The best solution I found for my question requiring the archiving of folders containing files was found in this question asked: create zip archive using NSTask containing a first level folder with the files

Using the zip exec in usr/bin/ like this

NSURL *destURL = self.archiveDestURL;
NSTask *task = [[NSTask alloc] init];
[task setCurrentDirectoryPath:[srcURL path]]; 
[task setLaunchPath:@"/usr/bin/zip"];
NSArray *argsArray = [NSArray arrayWithObjects:@"-r", @"-q", [destURL path], @".", @"-i", @"*", nil];
[task setArguments:argsArray];
[task launch];
[task waitUntilExit];
dkovacev
  • 33
  • 4