How to send a file hierarchy over a socket
So I am writing an application which will need to receive a file structure over a socket开发者_如何学Go from a server application (which I also have control over). My client is in objective-c and will need to construct an array of "File" objects. I figured that recursion would be a good way to go about this but I know that my implementation is incorrect.
Here is my original thinking:
I was planning on using a syntax to tell the client whether a node had any children (e.g folder) or it was a leaf (e.g file). I also thought of having a "lastleaf" syntax for the last leaf to stop the recursion. The syntax would be "type:path/to/file". Here is the rough algorithm that I came up with:
NSString* string = [self getStringFromSocket];
NSArray* split = [string componentsSeparatedByString:@":"];
if( [@"lastleaf" isEqualToString:[split objectAtIndex:0]] ) {
MyFile* newFile = [[MyFile alloc] initWithPath:[split subarrayWithRange:NSMakeRange(1, [split count]-1)]];
return [[NSMutableArray alloc] initWithObjects:newFile, nil];
} else if([@"leaf" isEqualToString:[split objectAtIndex:0]]) {
MyFile* newFile = [[MyFile alloc] initWithPath:[split subarrayWithRange:NSMakeRange(1, [split count]-1)]];
NSMutableArray* children = [[NSMutableArray alloc] initWithObjects:newFile, nil];
[children addObjectsFromArray: [self getFiles]];
return children;
} else if([@"parent" isEqualToString:[split objectAtIndex:0]]) {
MyFile* newFile = [[MyFile alloc] initWithPath:[split subarrayWithRange:NSMakeRange(1, [split count]-1)]];
[newFile setSubfiles: [self getFiles]];
return [[NSMutableArray alloc] initWithObjects:newFile, nil];
}
}
I'm happy for anyone to ignore this and to deal with pseudo-code, it was just my flawed thinking of doing things.
I would appreciate any suggestions or help.
Edit: I only want to see the path of the files and their structure, not the contents
A perfect way to encapsulate a filesystem structure is with nested NSDictionary
objects:
- There's no chance of infinite recursion because there's a finite number of key-value pairs
- If you want to add attributes to the files you can use a key syntax like
<filename>_attr_readonly
or<filename>_isFolder
, or you can even provide aserialize
anddeserialize
function on customFile
objects (containing the attributes and so forth) which convert them intoNSString
s, and use those strings as the corresponding value to the filename key - These can be serialized natively to a Plist file, and then you send the plist to the client (with compression, which will work extra well due to the content being text only)
To be slightly clearer:
NSDictionary (path: "/")
|
*-- key "myFile" -> value <MyFile> (path: "/myFile")
|
*-- key "myNestedFolder" -> value <NSDictionary>
|
*-- key "myNestedFile" -> value <MyFile>
(path "/myNestedFolder/myNestedFile")
I would send it in ZIP format. Takes care of the entire protocol issue and also adds compression. All you need is a suitable library.
Basically, you should leverage an existing class or library that manipulates file archive. That will have all the logic necessary for bundling together a file hierarchy and for unpacking it. Check this question for more info Cocoa class for TAR archiving and unarchiving files
精彩评论