Created
October 23, 2012 22:48
-
-
Save baado/3942278 to your computer and use it in GitHub Desktop.
File Management Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface OOFileManager : NSObject | |
+ (NSArray *)fileNames:(NSString *)fileName; | |
+ (BOOL)exists:(NSString *)fileName; | |
+ (BOOL)isDir:(NSString *)fileName; | |
+ (BOOL)resExists:(NSString *)fileName; | |
+ (void)makeDir:(NSString *)fileName; | |
+ (void)remove:(NSString *)fileName; | |
+ (BOOL)data2file:(NSData *)data fileName:(NSString *)fileName overwrite:(BOOL)overwrite; | |
+ (BOOL)data2file_add:(NSData *)data fileName:(NSString *)fileName; | |
+ (NSData *)file2data:(NSString *)fileName; | |
+ (NSData *)file2data:(NSString *)fileName offset:(unsigned long long)offset | |
length:(unsigned long long)length; | |
+ (NSData *)res2data:(NSString *)res; | |
+ (NSData *)res2data:(NSString *)res offset:(unsigned long long)offset | |
length:(unsigned long long)length; | |
+ (BOOL)res2file:(NSString *)res fileName:(NSString *)fileName overwrite:(BOOL)overwrite; | |
+ (void)copyItem:(NSString *)fromPath toPath:(NSString *)toPath overwrite:(BOOL)overwrite; | |
+ (void)moveItem:(NSString *)fromPath toPath:(NSString *)toPath; | |
+ (NSString *)getDocPath:(NSString*)fileName; | |
+ (NSString *)getAppPath:(NSString *)fileName; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "OOFileManager.h" | |
@implementation OOFileManager | |
//ファイル一覧の取得 | |
+ (NSArray*)fileNames:(NSString *)fileName | |
{ | |
if (![OOFileManager exists:fileName]) return Nil; | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; | |
} | |
//対象がディレクトリか | |
+ (BOOL)isDir:(NSString *)fileName | |
{ | |
BOOL isDir; | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]; | |
return isDir; | |
} | |
//ファイル・ディレクトリが存在するか | |
+ (BOOL)exists:(NSString *)fileName | |
{ | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
return [[NSFileManager defaultManager] fileExistsAtPath:path]; | |
} | |
//ファイル・ディレクトリが存在するか | |
+ (BOOL)resExists:(NSString *)fileName | |
{ | |
NSString *path = [OOFileManager getAppPath:fileName]; | |
return [[NSFileManager defaultManager] fileExistsAtPath:path]; | |
} | |
//アプリ内のパスを得る | |
+ (NSString *)getAppPath:(NSString *)fileName | |
{ | |
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@""]; | |
return path; | |
} | |
//プロジェクトフォルダのファイルのパス | |
+ (NSString *)getDocPath:(NSString *)fileName | |
{ | |
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; | |
path = [path stringByAppendingPathComponent:fileName]; | |
return path; | |
} | |
//ディレクトリの生成 | |
+ (void)makeDir:(NSString *)fileName | |
{ | |
if ([OOFileManager exists:fileName]) return; | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; | |
} | |
//ファイル・ディレクトリの移動 | |
+ (void)moveItem:(NSString *)fromPath toPath:(NSString *)toPath | |
{ | |
if (![OOFileManager exists:fromPath]) return; | |
if ([OOFileManager exists:toPath]) return; | |
NSString *pathFrom = [OOFileManager getDocPath:fromPath]; | |
NSString *pathTo = [OOFileManager getDocPath:toPath]; | |
[[NSFileManager defaultManager] moveItemAtPath:pathFrom toPath:pathTo error:nil]; | |
} | |
//ファイル・ディレクトリのコピー | |
+ (void)copyItem:(NSString *)fromPath toPath:(NSString *)toPath overwrite:(BOOL)overwrite | |
{ | |
if (![OOFileManager exists:fromPath]) return; | |
if ([OOFileManager exists:toPath]) { | |
//上書き禁止の場合 | |
if (!overwrite) { | |
return; | |
} | |
} | |
NSString *pathFrom = [OOFileManager getDocPath:fromPath]; | |
NSString *pathTo = [OOFileManager getDocPath:toPath]; | |
NSError *err = [[NSError alloc] init]; | |
if (overwrite) { | |
//旧ファイル・ディレクトリを削除 | |
[OOFileManager remove:toPath]; | |
} | |
[[NSFileManager defaultManager] copyItemAtPath:pathFrom toPath:pathTo error:&err]; | |
} | |
//ファイル・ディレクトリの削除 | |
+ (void)remove:(NSString *)fileName | |
{ | |
if (![OOFileManager exists:fileName]) return; | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
[[NSFileManager defaultManager] removeItemAtPath:path error:nil]; | |
} | |
//データ→ファイル | |
+ (BOOL)data2file:(NSData *)data fileName:(NSString *)fileName overwrite:(BOOL)overwrite | |
{ | |
if (data == Nil || data.length == 0) { | |
return NO; | |
} | |
if ([OOFileManager exists:fileName]) { | |
if (!overwrite) { | |
return NO; | |
} | |
} | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
if (overwrite) { | |
//旧ファイルを削除 | |
[OOFileManager remove:fileName]; | |
} | |
return ([data writeToFile:path atomically:YES]); | |
} | |
//データ→ファイル(追加) | |
+ (BOOL)data2file_add:(NSData *)data fileName:(NSString *)fileName | |
{ | |
if (![OOFileManager exists:fileName]) { | |
return NO; | |
} | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:path]; | |
if (fh) { | |
@try { | |
[fh seekToEndOfFile]; | |
[fh writeData:data]; | |
[fh closeFile]; | |
return YES; | |
} | |
@catch(id error) { | |
} | |
} | |
return NO; | |
} | |
//ファイル→データ | |
+ (NSData *)file2data:(NSString *)fileName | |
{ | |
if (![OOFileManager exists:fileName]) { | |
return Nil; | |
} | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
return [[NSData alloc] initWithContentsOfFile:path]; | |
} | |
//ファイル→データ(部分) | |
+ (NSData *)file2data:(NSString *)fileName offset:(unsigned long long)offset | |
length:(unsigned long long)length | |
{ | |
if (![OOFileManager exists:fileName]) { | |
return Nil; | |
} | |
NSString *path = [OOFileManager getDocPath:fileName]; | |
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:path]; | |
NSData *data; | |
if (fh) { | |
@try { | |
[fh seekToFileOffset:offset]; | |
data = [fh readDataOfLength:length]; | |
[fh closeFile]; | |
return data; | |
} @catch (id error) { | |
} | |
} | |
return nil; | |
} | |
//リソース→データ | |
+ (NSData *)res2data:(NSString *)res | |
{ | |
if (![OOFileManager resExists:res]) { | |
return Nil; | |
} | |
NSString *path = [OOFileManager getAppPath:res]; | |
return [NSData dataWithContentsOfFile:path]; | |
} | |
//リソース→データ(部分) | |
+ (NSData *)res2data:(NSString *)res offset:(unsigned long long)offset | |
length:(unsigned long long)length | |
{ | |
if (![OOFileManager resExists:res]) { | |
return Nil; | |
} | |
NSString *path = [OOFileManager getAppPath:res]; | |
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:path]; | |
NSData *data; | |
if (fh) { | |
@try { | |
[fh seekToFileOffset:offset]; | |
data=[fh readDataOfLength:length]; | |
[fh closeFile]; | |
return data; | |
} @catch (id error) { | |
} | |
} | |
return nil; | |
} | |
//リソース→ファイル | |
+ (BOOL)res2file:(NSString *)res fileName:(NSString *)fileName overwrite:(BOOL)overwrite | |
{ | |
if (![OOFileManager resExists:res]) { | |
return NO; | |
} | |
if ([OOFileManager exists:fileName]) { | |
//上書き禁止の場合 | |
if (!overwrite) { | |
return NO; | |
} | |
} | |
NSString *from = [OOFileManager getAppPath:res]; | |
NSString *to = [OOFileManager getDocPath:fileName]; | |
if (overwrite) { | |
//旧ファイル・ディレクトリを削除 | |
[OOFileManager remove:fileName]; | |
} | |
[[NSFileManager defaultManager] copyItemAtPath:from toPath:to error:nil]; | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fork from above URL.
http://www.saturn.dti.ne.jp/npaka/iphone/util/index.html