├── HYFileManager.podspec ├── LICENSE ├── HYFileManager ├── HYFileManager.h └── HYFileManager.m ├── .gitignore ├── Docs └── README_cn.md └── README.md /HYFileManager.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |spec| 2 | spec.name = 'HYFileManager' 3 | spec.version = '0.1.1' 4 | spec.license = { :type => 'MIT' } 5 | spec.homepage = 'https://github.com/castial/HYFileManager' 6 | spec.authors = { 'Hyyy' => 'liyangliyang001@gmail.com' } 7 | spec.summary = 'HYFileManager is a powerful util based on NSFileManager.' 8 | spec.source = { :git => 'https://github.com/castial/HYFileManager.git', :tag => '0.1.1' } 9 | spec.source_files = 'HYFileManager/*.{h,m}' 10 | spec.platform = :ios, '5.0' 11 | spec.framework = 'Foundation', 'UIKit' 12 | spec.requires_arc = true 13 | end 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /HYFileManager/HYFileManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // HYFileManager.h 3 | // HYFileManager 4 | // 5 | // Created by work on 15/9/30. 6 | // Copyright © 2015年 Hyyy. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface HYFileManager : NSObject 12 | 13 | #pragma mark - 沙盒目录相关 14 | // 沙盒的主目录路径 15 | + (NSString *)homeDir; 16 | // 沙盒中Documents的目录路径 17 | + (NSString *)documentsDir; 18 | // 沙盒中Library的目录路径 19 | + (NSString *)libraryDir; 20 | // 沙盒中Libarary/Preferences的目录路径 21 | + (NSString *)preferencesDir; 22 | // 沙盒中Library/Caches的目录路径 23 | + (NSString *)cachesDir; 24 | // 沙盒中tmp的目录路径 25 | + (NSString *)tmpDir; 26 | 27 | #pragma mark - 遍历文件夹 28 | /** 29 | 文件遍历 30 | 31 | @param path 目录的绝对路径 32 | @param deep 是否深遍历 (1. 浅遍历:返回当前目录下的所有文件和文件夹; 33 | 2. 深遍历:返回当前目录下及子目录下的所有文件和文件夹) 34 | @return 遍历结果数组 35 | */ 36 | + (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep; 37 | // 遍历沙盒主目录 38 | + (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep; 39 | // 遍历Documents目录 40 | + (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep; 41 | // 遍历Library目录 42 | + (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep; 43 | // 遍历Caches目录 44 | + (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep; 45 | // 遍历tmp目录 46 | + (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep; 47 | 48 | #pragma mark - 获取文件属性 49 | // 根据key获取文件某个属性 50 | + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key; 51 | // 根据key获取文件某个属性(错误信息error) 52 | + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key error:(NSError **)error; 53 | // 获取文件属性集合 54 | + (NSDictionary *)attributesOfItemAtPath:(NSString *)path; 55 | // 获取文件属性集合(错误信息error) 56 | + (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error; 57 | 58 | #pragma mark - 创建文件(夹) 59 | // 创建文件夹 60 | + (BOOL)createDirectoryAtPath:(NSString *)path; 61 | // 创建文件夹(错误信息error) 62 | + (BOOL)createDirectoryAtPath:(NSString *)path error:(NSError **)error; 63 | // 创建文件 64 | + (BOOL)createFileAtPath:(NSString *)path; 65 | // 创建文件(错误信息error) 66 | + (BOOL)createFileAtPath:(NSString *)path error:(NSError **)error; 67 | // 创建文件,是否覆盖 68 | + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite; 69 | // 创建文件,是否覆盖(错误信息error) 70 | + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite error:(NSError **)error; 71 | // 创建文件,文件内容 72 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content; 73 | // 创建文件,文件内容(错误信息error) 74 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error; 75 | // 创建文件,文件内容,是否覆盖 76 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite; 77 | // 创建文件,文件内容,是否覆盖(错误信息error) 78 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite error:(NSError **)error; 79 | // 获取创建文件时间 80 | + (NSDate *)creationDateOfItemAtPath:(NSString *)path; 81 | // 获取创建文件时间(错误信息error) 82 | + (NSDate *)creationDateOfItemAtPath:(NSString *)path error:(NSError **)error; 83 | // 获取文件修改时间 84 | + (NSDate *)modificationDateOfItemAtPath:(NSString *)path; 85 | // 获取文件修改时间(错误信息error) 86 | + (NSDate *)modificationDateOfItemAtPath:(NSString *)path error:(NSError **)error; 87 | 88 | #pragma mark - 删除文件(夹) 89 | // 删除文件 90 | + (BOOL)removeItemAtPath:(NSString *)path; 91 | // 删除文件(错误信息error) 92 | + (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error; 93 | // 清空Caches文件夹 94 | + (BOOL)clearCachesDirectory; 95 | // 清空tmp文件夹 96 | + (BOOL)clearTmpDirectory; 97 | 98 | #pragma mark - 复制文件(夹) 99 | // 复制文件 100 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath; 101 | // 复制文件(错误信息error) 102 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error; 103 | // 复制文件,是否覆盖 104 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite; 105 | // 复制文件,是否覆盖(错误信息error) 106 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error; 107 | 108 | #pragma mark - 移动文件(夹) 109 | // 移动文件 110 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath; 111 | // 移动文件(错误信息error) 112 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error; 113 | // 移动文件,是否覆盖 114 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite; 115 | // 移动文件,是否覆盖(错误信息error) 116 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error; 117 | 118 | #pragma mark - 根据URL获取文件名 119 | // 根据文件路径获取文件名称,是否需要后缀 120 | + (NSString *)fileNameAtPath:(NSString *)path suffix:(BOOL)suffix; 121 | // 获取文件所在的文件夹路径 122 | + (NSString *)directoryAtPath:(NSString *)path; 123 | // 根据文件路径获取文件扩展类型 124 | + (NSString *)suffixAtPath:(NSString *)path; 125 | 126 | #pragma mark - 判断文件(夹)是否存在 127 | // 判断文件路径是否存在 128 | + (BOOL)isExistsAtPath:(NSString *)path; 129 | // 判断路径是否为空(判空条件是文件大小为0,或者是文件夹下没有子文件) 130 | + (BOOL)isEmptyItemAtPath:(NSString *)path; 131 | // 判断路径是否为空(错误信息error) 132 | + (BOOL)isEmptyItemAtPath:(NSString *)path error:(NSError **)error; 133 | // 判断目录是否是文件夹 134 | + (BOOL)isDirectoryAtPath:(NSString *)path; 135 | // 判断目录是否是文件夹(错误信息error) 136 | + (BOOL)isDirectoryAtPath:(NSString *)path error:(NSError **)error; 137 | // 判断目录是否是文件 138 | + (BOOL)isFileAtPath:(NSString *)path; 139 | // 判断目录是否是文件(错误信息error) 140 | + (BOOL)isFileAtPath:(NSString *)path error:(NSError **)error; 141 | // 判断目录是否可以执行 142 | + (BOOL)isExecutableItemAtPath:(NSString *)path; 143 | // 判断目录是否可读 144 | + (BOOL)isReadableItemAtPath:(NSString *)path; 145 | // 判断目录是否可写 146 | + (BOOL)isWritableItemAtPath:(NSString *)path; 147 | 148 | #pragma mark - 获取文件(夹)大小 149 | // 获取目录大小 150 | + (NSNumber *)sizeOfItemAtPath:(NSString *)path; 151 | // 获取目录大小(错误信息error) 152 | + (NSNumber *)sizeOfItemAtPath:(NSString *)path error:(NSError **)error; 153 | // 获取文件大小 154 | + (NSNumber *)sizeOfFileAtPath:(NSString *)path; 155 | // 获取文件大小(错误信息error) 156 | + (NSNumber *)sizeOfFileAtPath:(NSString *)path error:(NSError **)error; 157 | // 获取文件夹大小 158 | + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path; 159 | // 获取文件夹大小(错误信息error) 160 | + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path error:(NSError **)error; 161 | 162 | // 获取目录大小,返回格式化后的数值 163 | + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path; 164 | // 获取目录大小,返回格式化后的数值(错误信息error) 165 | + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path error:(NSError **)error; 166 | // 获取文件大小,返回格式化后的数值 167 | + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path; 168 | // 获取文件大小,返回格式化后的数值(错误信息error) 169 | + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path error:(NSError **)error; 170 | // 获取文件夹大小,返回格式化后的数值 171 | + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path; 172 | // 获取文件夹大小,返回格式化后的数值(错误信息error) 173 | + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path error:(NSError **)error; 174 | 175 | #pragma mark - 写入文件内容 176 | // 写入文件内容 177 | + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content; 178 | // 写入文件内容(错误信息error) 179 | + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error; 180 | 181 | @end 182 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ######################### 2 | # .gitignore file for Xcode4 and Xcode5 Source projects 3 | # 4 | # Apple bugs, waiting for Apple to fix/respond: 5 | # 6 | # 15564624 - what does the xccheckout file in Xcode5 do? Where's the documentation? 7 | # 8 | # Version 2.6 9 | # For latest version, see: http://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects 10 | # 11 | # 2015 updates: 12 | # - Fixed typo in "xccheckout" line - thanks to @lyck for pointing it out! 13 | # - Fixed the .idea optional ignore. Thanks to @hashier for pointing this out 14 | # - Finally added "xccheckout" to the ignore. Apple still refuses to answer support requests about this, but in practice it seems you should ignore it. 15 | # - minor tweaks from Jona and Coeur (slightly more precise xc* filtering/names) 16 | # 2014 updates: 17 | # - appended non-standard items DISABLED by default (uncomment if you use those tools) 18 | # - removed the edit that an SO.com moderator made without bothering to ask me 19 | # - researched CocoaPods .lock more carefully, thanks to Gokhan Celiker 20 | # 2013 updates: 21 | # - fixed the broken "save personal Schemes" 22 | # - added line-by-line explanations for EVERYTHING (some were missing) 23 | # 24 | # NB: if you are storing "built" products, this WILL NOT WORK, 25 | # and you should use a different .gitignore (or none at all) 26 | # This file is for SOURCE projects, where there are many extra 27 | # files that we want to exclude 28 | # 29 | ######################### 30 | 31 | ##### 32 | # OS X temporary files that should never be committed 33 | # 34 | # c.f. http://www.westwind.com/reference/os-x/invisibles.html 35 | 36 | .DS_Store 37 | 38 | # c.f. http://www.westwind.com/reference/os-x/invisibles.html 39 | 40 | .Trashes 41 | 42 | # c.f. http://www.westwind.com/reference/os-x/invisibles.html 43 | 44 | *.swp 45 | 46 | # 47 | # *.lock - this is used and abused by many editors for many different things. 48 | # For the main ones I use (e.g. Eclipse), it should be excluded 49 | # from source-control, but YMMV. 50 | # (lock files are usually local-only file-synchronization on the local FS that should NOT go in git) 51 | # c.f. the "OPTIONAL" section at bottom though, for tool-specific variations! 52 | # 53 | # In particular, if you're using CocoaPods, you'll want to comment-out this line: 54 | *.lock 55 | 56 | 57 | # 58 | # profile - REMOVED temporarily (on double-checking, I can't find it in OS X docs?) 59 | #profile 60 | 61 | 62 | #### 63 | # Xcode temporary files that should never be committed 64 | # 65 | # NB: NIB/XIB files still exist even on Storyboard projects, so we want this... 66 | 67 | *~.nib 68 | 69 | 70 | #### 71 | # Xcode build files - 72 | # 73 | # NB: slash on the end, so we only remove the FOLDER, not any files that were badly named "DerivedData" 74 | 75 | DerivedData/ 76 | 77 | # NB: slash on the end, so we only remove the FOLDER, not any files that were badly named "build" 78 | 79 | build/ 80 | 81 | 82 | ##### 83 | # Xcode private settings (window sizes, bookmarks, breakpoints, custom executables, smart groups) 84 | # 85 | # This is complicated: 86 | # 87 | # SOMETIMES you need to put this file in version control. 88 | # Apple designed it poorly - if you use "custom executables", they are 89 | # saved in this file. 90 | # 99% of projects do NOT use those, so they do NOT want to version control this file. 91 | # ..but if you're in the 1%, comment out the line "*.pbxuser" 92 | 93 | # .pbxuser: http://lists.apple.com/archives/xcode-users/2004/Jan/msg00193.html 94 | 95 | *.pbxuser 96 | 97 | # .mode1v3: http://lists.apple.com/archives/xcode-users/2007/Oct/msg00465.html 98 | 99 | *.mode1v3 100 | 101 | # .mode2v3: http://lists.apple.com/archives/xcode-users/2007/Oct/msg00465.html 102 | 103 | *.mode2v3 104 | 105 | # .perspectivev3: http://stackoverflow.com/questions/5223297/xcode-projects-what-is-a-perspectivev3-file 106 | 107 | *.perspectivev3 108 | 109 | # NB: also, whitelist the default ones, some projects need to use these 110 | !default.pbxuser 111 | !default.mode1v3 112 | !default.mode2v3 113 | !default.perspectivev3 114 | 115 | 116 | #### 117 | # Xcode 4 - semi-personal settings 118 | # 119 | # Apple Shared data that Apple put in the wrong folder 120 | # c.f. http://stackoverflow.com/a/19260712/153422 121 | # FROM ANSWER: Apple says "don't ignore it" 122 | # FROM COMMENTS: Apple is wrong; Apple code is too buggy to trust; there are no known negative side-effects to ignoring Apple's unofficial advice and instead doing the thing that actively fixes bugs in Xcode 123 | # Up to you, but ... current advice: ignore it. 124 | *.xccheckout 125 | 126 | # 127 | # 128 | # OPTION 1: --------------------------------- 129 | # throw away ALL personal settings (including custom schemes! 130 | # - unless they are "shared") 131 | # As per build/ and DerivedData/, this ought to have a trailing slash 132 | # 133 | # NB: this is exclusive with OPTION 2 below 134 | xcuserdata/ 135 | 136 | # OPTION 2: --------------------------------- 137 | # get rid of ALL personal settings, but KEEP SOME OF THEM 138 | # - NB: you must manually uncomment the bits you want to keep 139 | # 140 | # NB: this *requires* git v1.8.2 or above; you may need to upgrade to latest OS X, 141 | # or manually install git over the top of the OS X version 142 | # NB: this is exclusive with OPTION 1 above 143 | # 144 | xcuserdata/**/* 145 | 146 | # (requires option 2 above): Personal Schemes 147 | # 148 | !xcuserdata/**/xcschemes/* 149 | 150 | #### 151 | # XCode 4 workspaces - more detailed 152 | # 153 | # Workspaces are important! They are a core feature of Xcode - don't exclude them :) 154 | # 155 | # Workspace layout is quite spammy. For reference: 156 | # 157 | # /(root)/ 158 | # /(project-name).xcodeproj/ 159 | # project.pbxproj 160 | # /project.xcworkspace/ 161 | # contents.xcworkspacedata 162 | # /xcuserdata/ 163 | # /(your name)/xcuserdatad/ 164 | # UserInterfaceState.xcuserstate 165 | # /xcshareddata/ 166 | # /xcschemes/ 167 | # (shared scheme name).xcscheme 168 | # /xcuserdata/ 169 | # /(your name)/xcuserdatad/ 170 | # (private scheme).xcscheme 171 | # xcschememanagement.plist 172 | # 173 | # 174 | 175 | #### 176 | # Xcode 4 - Deprecated classes 177 | # 178 | # Allegedly, if you manually "deprecate" your classes, they get moved here. 179 | # 180 | # We're using source-control, so this is a "feature" that we do not want! 181 | 182 | *.moved-aside 183 | 184 | #### 185 | # OPTIONAL: Some well-known tools that people use side-by-side with Xcode / iOS development 186 | # 187 | # NB: I'd rather not include these here, but gitignore's design is weak and doesn't allow 188 | # modular gitignore: you have to put EVERYTHING in one file. 189 | # 190 | # COCOAPODS: 191 | # 192 | # c.f. http://guides.cocoapods.org/using/using-cocoapods.html#what-is-a-podfilelock 193 | # c.f. http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 194 | # 195 | #!Podfile.lock 196 | # 197 | # RUBY: 198 | # 199 | # c.f. http://yehudakatz.com/2010/12/16/clarifying-the-roles-of-the-gemspec-and-gemfile/ 200 | # 201 | #!Gemfile.lock 202 | # 203 | # IDEA: 204 | # 205 | # c.f. https://www.jetbrains.com/objc/help/managing-projects-under-version-control.html?search=workspace.xml 206 | # 207 | #.idea/workspace.xml 208 | # 209 | # TEXTMATE: 210 | # 211 | # -- UNVERIFIED: c.f. http://stackoverflow.com/a/50283/153422 212 | # 213 | #tm_build_errors 214 | 215 | #### 216 | # UNKNOWN: recommended by others, but I can't discover what these files are 217 | # 218 | -------------------------------------------------------------------------------- /Docs/README_cn.md: -------------------------------------------------------------------------------- 1 | ### 简单实用的iOS文件工具类 2 | 3 | HYFileManager一个基于NSFileManager的文件操作类,它提供一系列的静态方法,只用少量的代码,来处理经常需要处理的文件操作,使得工作更加方便快捷。 4 | 5 | ### 要求 6 | - iOS >= 5.0 7 | - ARC环境 8 | 9 | ### 特性 10 | 11 | - 沙盒日常操作目录集合,简单语法即可获取目录路径; 12 | - 遍历文件夹,包含两种方式,深遍历和浅遍历; 13 | - 获取文件属性,包含单个属性获取方法和所有属性获取方法; 14 | - 创建文件(夹),创建文件夹,文件,可以根据所需,是否需要覆盖,是否需要默认内容来创建; 15 | - 删除文件(夹),提供两个静态方法来快速实现清空Caches和tmp文件夹内容; 16 | - 复制、移动文件(夹),可以选择是否需要覆盖来对文件(夹)进行复制和移动; 17 | - 根据目录路径来获取文件名和文件扩展类型; 18 | - 判断文件(夹)是否存在、文件(夹)判空、判断路径是否为文件或者文件夹、已经判断目录是否可读可写; 19 | - 获取文件(夹)的大小,提供两种返回方式,NSNumber和NSString,方便开发使用; 20 | - 写入文件内容,支持基本数据类型、NSData、UIImage和NSCoding类型。 21 | 22 | 查看 [HYFileManager.h](https://github.com/castial/HYFileManager/HYFileManager/HYFileManager.h),获取方法详细介绍。 23 | 24 | ### 安装 25 | 26 | #### CocoaPods: 27 | 28 | `pod 'FCFileManager'` 29 | 30 | #### 下载HYFileManager: 31 | 32 | 下载HYFileManager,将HYFileManager拖动到你的使用工程中即可。 33 | 34 | ### 使用实例 35 | 36 | #### 常见沙盒目录 37 | ``` 38 | /* 39 | All shortcuts suppported: 40 | 41 | + (NSString *)homeDir; 42 | + (NSString *)documentsDir; 43 | + (NSString *)libraryDir; 44 | + (NSString *)preferencesDir; 45 | + (NSString *)cachesDir; 46 | + (NSString *)tmpDir; 47 | */ 48 | 49 | // 沙盒目录 50 | NSString *homePath = [HYFileManager homeDir]; 51 | ``` 52 | #### 遍历文件夹 53 | ``` 54 | /* 55 | All shortcuts suppported: 56 | 57 | + (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep; 58 | + (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep; 59 | + (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep; 60 | + (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep; 61 | + (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep; 62 | + (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep; 63 | */ 64 | 65 | // 遍历library文件夹 66 | NSArray *libraryArr = [HYFileManager listFilesInLibraryDirectoryByDeep:NO]; 67 | ``` 68 | #### 获取文件属性 69 | ``` 70 | /* 71 | All shortcuts suppported: 72 | 73 | + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key; 74 | + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key error:(NSError **)error; 75 | + (NSDictionary *)attributesOfItemAtPath:(NSString *)path; 76 | + (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error; 77 | */ 78 | 79 | // 获取文件创建时间 80 | NSDate *date = (NSDate *)[HYFileManager attributeOfItemAtPath:path forKey:NSFileCreationDate error:error]; 81 | ``` 82 | #### 创建文件(夹) 83 | ``` 84 | /* 85 | All shortcuts suppported: 86 | 87 | + (BOOL)createDirectoryAtPath:(NSString *)path; 88 | + (BOOL)createDirectoryAtPath:(NSString *)path error:(NSError **)error; 89 | + (BOOL)createFileAtPath:(NSString *)path; 90 | + (BOOL)createFileAtPath:(NSString *)path error:(NSError **)error; 91 | + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite; 92 | + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite error:(NSError **)error; 93 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content; 94 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error; 95 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite; 96 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite error:(NSError **)error; 97 | + (NSDate *)creationDateOfItemAtPath:(NSString *)path; 98 | + (NSDate *)creationDateOfItemAtPath:(NSString *)path error:(NSError **)error; 99 | + (NSDate *)modificationDateOfItemAtPath:(NSString *)path; 100 | + (NSDate *)modificationDateOfItemAtPath:(NSString *)path error:(NSError **)error; 101 | */ 102 | 103 | // library下创建一个test文件夹 104 | NSString *directoryPath = [NSString stringWithFormat:@"%@/test", [HYFileManager libraryDir]]; 105 | BOOL isSuccess = [HYFileManager createDirectoryAtPath:directoryPath]; 106 | ``` 107 | #### 删除文件(夹) 108 | ``` 109 | /* 110 | All shortcuts suppported: 111 | 112 | + (BOOL)removeItemAtPath:(NSString *)path; 113 | + (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error; 114 | + (BOOL)clearCachesDirectory; 115 | + (BOOL)clearTmpDirectory; 116 | */ 117 | 118 | // 删除library下的test文件夹 119 | NSString *directoryPath = [NSString stringWithFormat:@"%@/test", [HYFileManager libraryDir]]; 120 | BOOL isSuccess = [HYFileManager removeItemAtPath:directoryPath]; 121 | ``` 122 | #### 复制文件(夹) 123 | ``` 124 | /* 125 | All shortcuts suppported: 126 | 127 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath; 128 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error; 129 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite; 130 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error; 131 | */ 132 | 133 | // 复制文件夹 134 | NSError *error; 135 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy", [HYFileManager libraryDir]]; 136 | NSString *toPath = [NSString stringWithFormat:@"%@/hyyy", [HYFileManager libraryDir]]; 137 | BOOL isSuccess = [HYFileManager copyItemAtPath:path toPath:toPath overwrite:YES error:&error]; 138 | ``` 139 | #### 移动文件(夹) 140 | ``` 141 | /* 142 | All shortcuts suppported: 143 | 144 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath; 145 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error; 146 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite; 147 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error; 148 | */ 149 | 150 | // 移动文件夹 151 | NSError *error; 152 | NSString *path = [NSString stringWithFormat:@"%@/hyyy", [HYFileManager libraryDir]]; 153 | NSString *toPath = [NSString stringWithFormat:@"%@/test/hyyy", [HYFileManager libraryDir]]; 154 | BOOL isSuccess = [HYFileManager moveItemAtPath:path toPath:toPath overwrite:YES error:&error]; 155 | ``` 156 | #### 获取文件名和扩展类型 157 | ``` 158 | /* 159 | All shortcuts suppported: 160 | 161 | + (NSString *)fileNameAtPath:(NSString *)path suffix:(BOOL)suffix; 162 | + (NSString *)directoryAtPath:(NSString *)path; 163 | + (NSString *)suffixAtPath:(NSString *)path; 164 | 165 | */ 166 | 167 | // 获取文件夹名称,带后缀 168 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; 169 | NSString *fileName = [HYFileManager fileNameAtPath:path suffix:YES]; 170 | ``` 171 | #### 文件(夹)是否存在、判空和可读可写 172 | ``` 173 | /* 174 | All shortcuts suppported: 175 | 176 | + (BOOL)isExistsAtPath:(NSString *)path; 177 | + (BOOL)isEmptyItemAtPath:(NSString *)path; 178 | + (BOOL)isEmptyItemAtPath:(NSString *)path error:(NSError **)error; 179 | + (BOOL)isDirectoryAtPath:(NSString *)path; 180 | + (BOOL)isDirectoryAtPath:(NSString *)path error:(NSError **)error; 181 | + (BOOL)isFileAtPath:(NSString *)path; 182 | + (BOOL)isFileAtPath:(NSString *)path error:(NSError **)error; 183 | + (BOOL)isExecutableItemAtPath:(NSString *)path; 184 | + (BOOL)isReadableItemAtPath:(NSString *)path; 185 | + (BOOL)isWritableItemAtPath:(NSString *)path; 186 | */ 187 | 188 | // 判断目录是否存在 189 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; 190 | BOOL isExist = [HYFileManager isExistsAtPath:path]; 191 | ``` 192 | #### 获取文件(夹)大小 193 | ``` 194 | /* 195 | All shortcuts suppported: 196 | 197 | + (NSNumber *)sizeOfItemAtPath:(NSString *)path; 198 | + (NSNumber *)sizeOfItemAtPath:(NSString *)path error:(NSError **)error; 199 | + (NSNumber *)sizeOfFileAtPath:(NSString *)path; 200 | + (NSNumber *)sizeOfFileAtPath:(NSString *)path error:(NSError **)error; 201 | + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path; 202 | + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path error:(NSError **)error; 203 | 204 | + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path; 205 | + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path error:(NSError **)error; 206 | + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path; 207 | + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path error:(NSError **)error; 208 | + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path; 209 | + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path error:(NSError **)error; 210 | */ 211 | 212 | // 获取文件大小 213 | NSError *error; 214 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; 215 | NSNumber *size = [HYFileManager sizeOfFileAtPath:path error:&error]; 216 | ``` 217 | #### 写入文件内容 218 | ``` 219 | /* 220 | All shortcuts suppported: 221 | 222 | + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content; 223 | + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error; 224 | */ 225 | 226 | // 遍历library文件夹 227 | NSError *error; 228 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; 229 | BOOL isSuccess = [HYFileManager writeFileAtPath:path content:@"Hello World" error:error]; 230 | ``` 231 | 232 | ##License 233 | Released under [MIT License](LICENSE). 234 | 235 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HYFileManager ![Pod version](http://img.shields.io/cocoapods/v/HYFileManager.svg) ![Pod platforms](http://img.shields.io/cocoapods/p/HYFileManager.svg) ![Pod license](http://img.shields.io/cocoapods/l/HYFileManager.svg) 2 | 3 | ### A Practical Tools Of File Operations 4 | 5 | HYFileManager is a simple tool of iOS file operations base on NSFileManager. It offers a range of static methods, only a small amount of code to handle file operations often need to deal with, making work easier and faster. 6 | 7 | [**中文说明**](Docs/README_cn.md) 8 | ### Requirements 9 | - iOS >= 5.0 10 | - ARC enabled 11 | 12 | ### Features 13 | - operate sandbox directory, use a simple syntax can be operated sandbox directory; 14 | - traverse folder, based on deep values, and access the list of subdirectories; 15 | - Get the file attributes, Including a list of all the attributes or a single attribute; 16 | - Create a file, including the creation of folders, you can also choose whether content or overwrite parameters; 17 | - Delete file, including deleted folder, two new static methods, you can clear the caches and tmp folders 18 | - Copy and move file, including folder, you can easily move and copy the file; 19 | - Get the filename and extension type; 20 | - Determine whether a file exists, whether the file is empty, and whether the file is readable and writable; 21 | - Get file size,include folder size, and provide two ways to return; 22 | - Write to the file contents, support for basic data types, NSData, UIImage and NSCoding. 23 | 24 | See [HYFileManager.h](https://github.com/castial/HYFileManager/HYFileManager/HYFileManager.h), and get detailed methodology description. 25 | 26 | ### Installation 27 | 28 | #### CocoaPods: 29 | 30 | `pod 'FCFileManager'` 31 | 32 | #### Manual install: 33 | 34 | Copy `FCFileManager.h` and `FCFileManager.m` to your project. 35 | 36 | ### Usage examples 37 | 38 | #### Common sandbox directory 39 | 40 | ``` 41 | /* 42 | All shortcuts suppported: 43 | 44 | + (NSString *)homeDir; 45 | + (NSString *)documentsDir; 46 | + (NSString *)libraryDir; 47 | + (NSString *)preferencesDir; 48 | + (NSString *)cachesDir; 49 | + (NSString *)tmpDir; 50 | */ 51 | 52 | // home path 53 | NSString *homePath = [HYFileManager homeDir]; 54 | ``` 55 | 56 | #### Traverse folder 57 | 58 | ``` 59 | /* 60 | All shortcuts suppported: 61 | 62 | + (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep; 63 | + (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep; 64 | + (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep; 65 | + (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep; 66 | + (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep; 67 | + (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep; 68 | */ 69 | 70 | // Traverse library folder 71 | NSArray *libraryArr = [HYFileManager listFilesInLibraryDirectoryByDeep:NO]; 72 | ``` 73 | #### Get the file attributes 74 | ``` 75 | /* 76 | All shortcuts suppported: 77 | 78 | + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key; 79 | + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key error:(NSError **)error; 80 | + (NSDictionary *)attributesOfItemAtPath:(NSString *)path; 81 | + (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError **)error; 82 | */ 83 | 84 | // Get the file creation date 85 | NSDate *date = (NSDate *)[HYFileManager attributeOfItemAtPath:path forKey:NSFileCreationDate error:error]; 86 | ``` 87 | #### Create file or folder 88 | ``` 89 | /* 90 | All shortcuts suppported: 91 | 92 | + (BOOL)createDirectoryAtPath:(NSString *)path; 93 | + (BOOL)createDirectoryAtPath:(NSString *)path error:(NSError **)error; 94 | + (BOOL)createFileAtPath:(NSString *)path; 95 | + (BOOL)createFileAtPath:(NSString *)path error:(NSError **)error; 96 | + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite; 97 | + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite error:(NSError **)error; 98 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content; 99 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error; 100 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite; 101 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite error:(NSError **)error; 102 | + (NSDate *)creationDateOfItemAtPath:(NSString *)path; 103 | + (NSDate *)creationDateOfItemAtPath:(NSString *)path error:(NSError **)error; 104 | + (NSDate *)modificationDateOfItemAtPath:(NSString *)path; 105 | + (NSDate *)modificationDateOfItemAtPath:(NSString *)path error:(NSError **)error; 106 | */ 107 | 108 | // In the library folder, create a folder named test. 109 | NSString *directoryPath = [NSString stringWithFormat:@"%@/test", [HYFileManager libraryDir]]; 110 | BOOL isSuccess = [HYFileManager createDirectoryAtPath:directoryPath]; 111 | ``` 112 | #### Delete file or folder 113 | ``` 114 | /* 115 | All shortcuts suppported: 116 | 117 | + (BOOL)removeItemAtPath:(NSString *)path; 118 | + (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error; 119 | + (BOOL)clearCachesDirectory; 120 | + (BOOL)clearTmpDirectory; 121 | */ 122 | 123 | // In the library folder, delete a folder named test. 124 | NSString *directoryPath = [NSString stringWithFormat:@"%@/test", [HYFileManager libraryDir]]; 125 | BOOL isSuccess = [HYFileManager removeItemAtPath:directoryPath]; 126 | ``` 127 | #### Copy file or folder 128 | ``` 129 | /* 130 | All shortcuts suppported: 131 | 132 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath; 133 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error; 134 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite; 135 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error; 136 | */ 137 | 138 | // copy folder 139 | NSError *error; 140 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy", [HYFileManager libraryDir]]; 141 | NSString *toPath = [NSString stringWithFormat:@"%@/hyyy", [HYFileManager libraryDir]]; 142 | BOOL isSuccess = [HYFileManager copyItemAtPath:path toPath:toPath overwrite:YES error:&error]; 143 | ``` 144 | #### Move file or folder 145 | ``` 146 | /* 147 | All shortcuts suppported: 148 | 149 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath; 150 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError **)error; 151 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite; 152 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError **)error; 153 | */ 154 | 155 | // move folder 156 | NSError *error; 157 | NSString *path = [NSString stringWithFormat:@"%@/hyyy", [HYFileManager libraryDir]]; 158 | NSString *toPath = [NSString stringWithFormat:@"%@/test/hyyy", [HYFileManager libraryDir]]; 159 | BOOL isSuccess = [HYFileManager moveItemAtPath:path toPath:toPath overwrite:YES error:&error]; 160 | ``` 161 | #### Get the filename and extension type 162 | ``` 163 | /* 164 | All shortcuts suppported: 165 | 166 | + (NSString *)fileNameAtPath:(NSString *)path suffix:(BOOL)suffix; 167 | + (NSString *)directoryAtPath:(NSString *)path; 168 | + (NSString *)suffixAtPath:(NSString *)path; 169 | 170 | */ 171 | 172 | // get the filename, including suffix. 173 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; 174 | NSString *fileName = [HYFileManager fileNameAtPath:path suffix:YES]; 175 | ``` 176 | #### Determine whether a file exists 177 | ``` 178 | /* 179 | All shortcuts suppported: 180 | 181 | + (BOOL)isExistsAtPath:(NSString *)path; 182 | + (BOOL)isEmptyItemAtPath:(NSString *)path; 183 | + (BOOL)isEmptyItemAtPath:(NSString *)path error:(NSError **)error; 184 | + (BOOL)isDirectoryAtPath:(NSString *)path; 185 | + (BOOL)isDirectoryAtPath:(NSString *)path error:(NSError **)error; 186 | + (BOOL)isFileAtPath:(NSString *)path; 187 | + (BOOL)isFileAtPath:(NSString *)path error:(NSError **)error; 188 | + (BOOL)isExecutableItemAtPath:(NSString *)path; 189 | + (BOOL)isReadableItemAtPath:(NSString *)path; 190 | + (BOOL)isWritableItemAtPath:(NSString *)path; 191 | */ 192 | 193 | // whether a file exists 194 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; 195 | BOOL isExist = [HYFileManager isExistsAtPath:path]; 196 | ``` 197 | #### Get file size 198 | ``` 199 | /* 200 | All shortcuts suppported: 201 | 202 | + (NSNumber *)sizeOfItemAtPath:(NSString *)path; 203 | + (NSNumber *)sizeOfItemAtPath:(NSString *)path error:(NSError **)error; 204 | + (NSNumber *)sizeOfFileAtPath:(NSString *)path; 205 | + (NSNumber *)sizeOfFileAtPath:(NSString *)path error:(NSError **)error; 206 | + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path; 207 | + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path error:(NSError **)error; 208 | 209 | + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path; 210 | + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path error:(NSError **)error; 211 | + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path; 212 | + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path error:(NSError **)error; 213 | + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path; 214 | + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path error:(NSError **)error; 215 | */ 216 | 217 | // get file size. 218 | NSError *error; 219 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; 220 | NSNumber *size = [HYFileManager sizeOfFileAtPath:path error:&error]; 221 | ``` 222 | #### Write to the file contents 223 | ``` 224 | /* 225 | All shortcuts suppported: 226 | 227 | + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content; 228 | + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError **)error; 229 | */ 230 | 231 | // write content 232 | NSError *error; 233 | NSString *path = [NSString stringWithFormat:@"%@/test/hyyy/file.md", [HYFileManager libraryDir]]; 234 | BOOL isSuccess = [HYFileManager writeFileAtPath:path content:@"Hello World" error:error]; 235 | ``` 236 | 237 | ##License 238 | Released under [MIT License](LICENSE). 239 | 240 | -------------------------------------------------------------------------------- /HYFileManager/HYFileManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // HYFileManager.m 3 | // HYFileManager 4 | // 5 | // Created by work on 15/9/30. 6 | // Copyright © 2015年 Hyyy. All rights reserved. 7 | // 8 | 9 | #import "HYFileManager.h" 10 | 11 | @interface HYFileManager() 12 | 13 | @property (strong, nonatomic) NSFileManager *manager; 14 | 15 | @end 16 | 17 | @implementation HYFileManager 18 | 19 | #pragma mark - 沙盒目录相关 20 | + (NSString *)homeDir { 21 | return NSHomeDirectory(); 22 | } 23 | 24 | + (NSString *)documentsDir { 25 | return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 26 | } 27 | 28 | + (NSString *)libraryDir { 29 | return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];; 30 | } 31 | 32 | + (NSString *)preferencesDir { 33 | NSString *libraryDir = [self libraryDir]; 34 | return [libraryDir stringByAppendingPathComponent:@"Preferences"]; 35 | } 36 | 37 | + (NSString *)cachesDir { 38 | return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; 39 | } 40 | 41 | + (NSString *)tmpDir { 42 | return NSTemporaryDirectory(); 43 | } 44 | #pragma mark - 遍历文件夹 45 | + (NSArray *)listFilesInDirectoryAtPath:(NSString *)path deep:(BOOL)deep { 46 | NSArray *listArr; 47 | NSError *error; 48 | NSFileManager *manager = [NSFileManager defaultManager]; 49 | if (deep) { 50 | // 深遍历 51 | NSArray *deepArr = [manager subpathsOfDirectoryAtPath:path error:&error]; 52 | if (!error) { 53 | listArr = deepArr; 54 | }else { 55 | listArr = nil; 56 | } 57 | }else { 58 | // 浅遍历 59 | NSArray *shallowArr = [manager contentsOfDirectoryAtPath:path error:&error]; 60 | if (!error) { 61 | listArr = shallowArr; 62 | }else { 63 | listArr = nil; 64 | } 65 | } 66 | return listArr; 67 | } 68 | 69 | + (NSArray *)listFilesInHomeDirectoryByDeep:(BOOL)deep { 70 | return [self listFilesInDirectoryAtPath:[self homeDir] deep:deep]; 71 | } 72 | 73 | + (NSArray *)listFilesInLibraryDirectoryByDeep:(BOOL)deep { 74 | return [self listFilesInDirectoryAtPath:[self libraryDir] deep:deep]; 75 | } 76 | 77 | + (NSArray *)listFilesInDocumentDirectoryByDeep:(BOOL)deep { 78 | return [self listFilesInDirectoryAtPath:[self documentsDir] deep:deep]; 79 | } 80 | 81 | + (NSArray *)listFilesInTmpDirectoryByDeep:(BOOL)deep { 82 | return [self listFilesInDirectoryAtPath:[self tmpDir] deep:deep]; 83 | } 84 | 85 | + (NSArray *)listFilesInCachesDirectoryByDeep:(BOOL)deep { 86 | return [self listFilesInDirectoryAtPath:[self cachesDir] deep:deep]; 87 | } 88 | 89 | #pragma mark - 获取文件属性 90 | + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key { 91 | return [[self attributesOfItemAtPath:path] objectForKey:key]; 92 | } 93 | 94 | + (id)attributeOfItemAtPath:(NSString *)path forKey:(NSString *)key error:(NSError *__autoreleasing *)error { 95 | return [[self attributesOfItemAtPath:path error:error] objectForKey:key]; 96 | } 97 | 98 | + (NSDictionary *)attributesOfItemAtPath:(NSString *)path { 99 | return [self attributesOfItemAtPath:path error:nil]; 100 | } 101 | 102 | + (NSDictionary *)attributesOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 103 | return [[NSFileManager defaultManager] attributesOfItemAtPath:path error:error]; 104 | } 105 | 106 | #pragma mark - 创建文件(夹) 107 | + (BOOL)createDirectoryAtPath:(NSString *)path { 108 | return [self createDirectoryAtPath:path error:nil]; 109 | } 110 | 111 | + (BOOL)createDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 112 | NSFileManager *manager = [NSFileManager defaultManager]; 113 | BOOL isSuccess = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:error]; 114 | return isSuccess; 115 | } 116 | 117 | + (BOOL)createFileAtPath:(NSString *)path { 118 | return [self createFileAtPath:path content:nil overwrite:YES error:nil]; 119 | } 120 | 121 | + (BOOL)createFileAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 122 | return [self createFileAtPath:path content:nil overwrite:YES error:error]; 123 | } 124 | 125 | + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite { 126 | return [self createFileAtPath:path content:nil overwrite:overwrite error:nil]; 127 | } 128 | 129 | + (BOOL)createFileAtPath:(NSString *)path overwrite:(BOOL)overwrite error:(NSError *__autoreleasing *)error { 130 | return [self createFileAtPath:path content:nil overwrite:overwrite error:error]; 131 | } 132 | 133 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content { 134 | return [self createFileAtPath:path content:content overwrite:YES error:nil]; 135 | } 136 | 137 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError *__autoreleasing *)error { 138 | return [self createFileAtPath:path content:content overwrite:YES error:error]; 139 | } 140 | 141 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite { 142 | return [self createFileAtPath:path content:content overwrite:overwrite]; 143 | } 144 | 145 | + (BOOL)createFileAtPath:(NSString *)path content:(NSObject *)content overwrite:(BOOL)overwrite error:(NSError *__autoreleasing *)error { 146 | // 如果文件夹路径不存在,那么先创建文件夹 147 | NSString *directoryPath = [self directoryAtPath:path]; 148 | if (![self isExistsAtPath:directoryPath]) { 149 | // 创建文件夹 150 | if (![self createDirectoryAtPath:directoryPath error:error]) { 151 | return NO; 152 | } 153 | } 154 | // 如果文件存在,并不想覆盖,那么直接返回YES。 155 | if (!overwrite) { 156 | if ([self isExistsAtPath:path]) { 157 | return YES; 158 | } 159 | } 160 | // 创建文件 161 | BOOL isSuccess = [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil]; 162 | if (content) { 163 | [self writeFileAtPath:path content:content error:error]; 164 | } 165 | return isSuccess; 166 | } 167 | 168 | + (NSDate *)creationDateOfItemAtPath:(NSString *)path { 169 | return [self creationDateOfItemAtPath:path error:nil]; 170 | } 171 | 172 | + (NSDate *)creationDateOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 173 | return (NSDate *)[self attributeOfItemAtPath:path forKey:NSFileCreationDate error:error]; 174 | } 175 | 176 | + (NSDate *)modificationDateOfItemAtPath:(NSString *)path { 177 | return [self modificationDateOfItemAtPath:path error:nil]; 178 | } 179 | 180 | + (NSDate *)modificationDateOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 181 | return (NSDate *)[self attributeOfItemAtPath:path forKey:NSFileModificationDate error:error]; 182 | } 183 | 184 | #pragma mark - 删除文件(夹) 185 | + (BOOL)removeItemAtPath:(NSString *)path { 186 | return [self removeItemAtPath:path error:nil]; 187 | } 188 | 189 | + (BOOL)removeItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 190 | return [[NSFileManager defaultManager] removeItemAtPath:path error:error]; 191 | } 192 | 193 | + (BOOL)clearCachesDirectory { 194 | NSArray *subFiles = [self listFilesInCachesDirectoryByDeep:NO]; 195 | BOOL isSuccess = YES; 196 | 197 | for (NSString *file in subFiles) { 198 | NSString *absolutePath = [[self cachesDir] stringByAppendingPathComponent:file]; 199 | isSuccess &= [self removeItemAtPath:absolutePath]; 200 | } 201 | return isSuccess; 202 | } 203 | 204 | + (BOOL)clearTmpDirectory { 205 | NSArray *subFiles = [self listFilesInTmpDirectoryByDeep:NO]; 206 | BOOL isSuccess = YES; 207 | 208 | for (NSString *file in subFiles) { 209 | NSString *absolutePath = [[self tmpDir] stringByAppendingPathComponent:file]; 210 | isSuccess &= [self removeItemAtPath:absolutePath]; 211 | } 212 | return isSuccess; 213 | } 214 | 215 | #pragma mark - 复制文件(夹) 216 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath { 217 | return [self copyItemAtPath:path toPath:toPath overwrite:NO error:nil]; 218 | } 219 | 220 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError *__autoreleasing *)error { 221 | return [self copyItemAtPath:path toPath:toPath overwrite:NO error:error]; 222 | } 223 | 224 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite { 225 | return [self copyItemAtPath:path toPath:toPath overwrite:overwrite error:nil]; 226 | } 227 | 228 | + (BOOL)copyItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError *__autoreleasing *)error { 229 | // 先要保证源文件路径存在,不然抛出异常 230 | if (![self isExistsAtPath:path]) { 231 | [NSException raise:@"非法的源文件路径" format:@"源文件路径%@不存在,请检查源文件路径", path]; 232 | return NO; 233 | } 234 | NSString *toDirPath = [self directoryAtPath:toPath]; 235 | if (![self isExistsAtPath:toDirPath]) { 236 | // 创建复制路径 237 | if (![self createDirectoryAtPath:toDirPath error:error]) { 238 | return NO; 239 | } 240 | } 241 | // 如果覆盖,那么先删掉原文件 242 | if (overwrite) { 243 | if ([self isExistsAtPath:toPath]) { 244 | [self removeItemAtPath:toPath error:error]; 245 | } 246 | } 247 | // 复制文件 248 | BOOL isSuccess = [[NSFileManager defaultManager] copyItemAtPath:path toPath:toPath error:error]; 249 | 250 | return isSuccess; 251 | } 252 | 253 | #pragma mark - 移动文件(夹) 254 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath { 255 | return [self moveItemAtPath:path toPath:toPath overwrite:NO error:nil]; 256 | } 257 | 258 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath error:(NSError *__autoreleasing *)error { 259 | return [self moveItemAtPath:path toPath:toPath overwrite:NO error:error]; 260 | } 261 | 262 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite { 263 | return [self moveItemAtPath:path toPath:toPath overwrite:overwrite error:nil]; 264 | } 265 | 266 | + (BOOL)moveItemAtPath:(NSString *)path toPath:(NSString *)toPath overwrite:(BOOL)overwrite error:(NSError *__autoreleasing *)error { 267 | // 先要保证源文件路径存在,不然抛出异常 268 | if (![self isExistsAtPath:path]) { 269 | [NSException raise:@"非法的源文件路径" format:@"源文件路径%@不存在,请检查源文件路径", path]; 270 | return NO; 271 | } 272 | NSString *toDirPath = [self directoryAtPath:toPath]; 273 | if (![self isExistsAtPath:toDirPath]) { 274 | // 创建移动路径 275 | if (![self createDirectoryAtPath:toDirPath error:error]) { 276 | return NO; 277 | } 278 | } 279 | // 如果覆盖,那么先删掉原文件 280 | if ([self isExistsAtPath:toPath]) { 281 | if (overwrite) { 282 | [self removeItemAtPath:toPath error:error]; 283 | }else { 284 | [self removeItemAtPath:path error:error]; 285 | return YES; 286 | } 287 | } 288 | 289 | // 移动文件 290 | BOOL isSuccess = [[NSFileManager defaultManager] moveItemAtPath:path toPath:toPath error:error]; 291 | 292 | return isSuccess; 293 | } 294 | 295 | #pragma mark - 根据URL获取文件名 296 | + (NSString *)fileNameAtPath:(NSString *)path suffix:(BOOL)suffix { 297 | NSString *fileName = [path lastPathComponent]; 298 | if (!suffix) { 299 | fileName = [fileName stringByDeletingPathExtension]; 300 | } 301 | return fileName; 302 | } 303 | 304 | + (NSString *)directoryAtPath:(NSString *)path { 305 | return [path stringByDeletingLastPathComponent]; 306 | } 307 | 308 | + (NSString *)suffixAtPath:(NSString *)path { 309 | return [path pathExtension]; 310 | } 311 | 312 | #pragma mark - 判断文件(夹)是否存在 313 | + (BOOL)isExistsAtPath:(NSString *)path { 314 | return [[NSFileManager defaultManager] fileExistsAtPath:path]; 315 | } 316 | 317 | + (BOOL)isEmptyItemAtPath:(NSString *)path { 318 | return [self isEmptyItemAtPath:path error:nil]; 319 | } 320 | 321 | + (BOOL)isEmptyItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 322 | return ([self isFileAtPath:path error:error] && 323 | [[self sizeOfItemAtPath:path error:error] intValue] == 0) || 324 | ([self isDirectoryAtPath:path error:error] && 325 | [[self listFilesInDirectoryAtPath:path deep:NO] count] == 0); 326 | } 327 | 328 | + (BOOL)isDirectoryAtPath:(NSString *)path { 329 | return [self isDirectoryAtPath:path error:nil]; 330 | } 331 | 332 | + (BOOL)isDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 333 | return ([self attributeOfItemAtPath:path forKey:NSFileType error:error] == NSFileTypeDirectory); 334 | } 335 | 336 | + (BOOL)isFileAtPath:(NSString *)path { 337 | return [self isFileAtPath:path error:nil]; 338 | } 339 | 340 | + (BOOL)isFileAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 341 | return ([self attributeOfItemAtPath:path forKey:NSFileType error:error] == NSFileTypeRegular); 342 | } 343 | 344 | + (BOOL)isExecutableItemAtPath:(NSString *)path { 345 | return [[NSFileManager defaultManager] isExecutableFileAtPath:path]; 346 | } 347 | 348 | + (BOOL)isReadableItemAtPath:(NSString *)path { 349 | return [[NSFileManager defaultManager] isReadableFileAtPath:path]; 350 | } 351 | + (BOOL)isWritableItemAtPath:(NSString *)path { 352 | return [[NSFileManager defaultManager] isWritableFileAtPath:path]; 353 | } 354 | 355 | #pragma mark - 获取文件(夹)大小 356 | + (NSNumber *)sizeOfItemAtPath:(NSString *)path { 357 | return [self sizeOfItemAtPath:path error:nil]; 358 | } 359 | 360 | + (NSNumber *)sizeOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 361 | return (NSNumber *)[self attributeOfItemAtPath:path forKey:NSFileSize error:error]; 362 | } 363 | 364 | + (NSNumber *)sizeOfFileAtPath:(NSString *)path { 365 | return [self sizeOfFileAtPath:path error:nil]; 366 | } 367 | 368 | + (NSNumber *)sizeOfFileAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 369 | if ([self isFileAtPath:path error:error]) { 370 | return [self sizeOfItemAtPath:path error:error]; 371 | } 372 | return nil; 373 | } 374 | 375 | + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path { 376 | return [self sizeOfDirectoryAtPath:path error:nil]; 377 | } 378 | 379 | + (NSNumber *)sizeOfDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 380 | if ([self isDirectoryAtPath:path error:error]) { 381 | NSNumber *size = [self sizeOfItemAtPath:path error:error]; 382 | double sizeValue = [size doubleValue]; 383 | 384 | NSArray *subPaths = [self listFilesInDirectoryAtPath:path deep:YES]; 385 | for (NSUInteger i = 0; i < subPaths.count; i++) { 386 | NSString *subPath = [subPaths objectAtIndex:i]; 387 | NSNumber *subPathSize = [self sizeOfItemAtPath:subPath error:error]; 388 | sizeValue += [subPathSize doubleValue]; 389 | } 390 | return [NSNumber numberWithDouble:sizeValue]; 391 | } 392 | return nil; 393 | } 394 | 395 | + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path { 396 | return [self sizeFormattedOfItemAtPath:path error:nil]; 397 | } 398 | 399 | + (NSString *)sizeFormattedOfItemAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 400 | NSNumber *size = [self sizeOfItemAtPath:path error:error]; 401 | if (!size) { 402 | return [self sizeFormatted:size]; 403 | } 404 | return nil; 405 | } 406 | 407 | + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path { 408 | return [self sizeFormattedOfFileAtPath:path error:nil]; 409 | } 410 | 411 | + (NSString *)sizeFormattedOfFileAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 412 | NSNumber *size = [self sizeOfFileAtPath:path error:error]; 413 | if (!size) { 414 | return [self sizeFormatted:size]; 415 | } 416 | return nil; 417 | } 418 | 419 | + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path { 420 | return [self sizeFormattedOfDirectoryAtPath:path error:nil]; 421 | } 422 | 423 | + (NSString *)sizeFormattedOfDirectoryAtPath:(NSString *)path error:(NSError *__autoreleasing *)error { 424 | NSNumber *size = [self sizeOfDirectoryAtPath:path error:error]; 425 | if (!size) { 426 | return [self sizeFormatted:size]; 427 | } 428 | return nil; 429 | } 430 | 431 | #pragma mark - 写入文件内容 432 | + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content { 433 | return [self writeFileAtPath:path content:content error:nil]; 434 | } 435 | 436 | + (BOOL)writeFileAtPath:(NSString *)path content:(NSObject *)content error:(NSError *__autoreleasing *)error { 437 | if (!content) { 438 | [NSException raise:@"非法的文件内容" format:@"文件内容不能为nil"]; 439 | return NO; 440 | } 441 | if ([self isExistsAtPath:path]) { 442 | if ([content isKindOfClass:[NSMutableArray class]]) { 443 | [(NSMutableArray *)content writeToFile:path atomically:YES]; 444 | }else if ([content isKindOfClass:[NSArray class]]) { 445 | [(NSArray *)content writeToFile:path atomically:YES]; 446 | }else if ([content isKindOfClass:[NSMutableData class]]) { 447 | [(NSMutableData *)content writeToFile:path atomically:YES]; 448 | }else if ([content isKindOfClass:[NSData class]]) { 449 | [(NSData *)content writeToFile:path atomically:YES]; 450 | }else if ([content isKindOfClass:[NSMutableDictionary class]]) { 451 | [(NSMutableDictionary *)content writeToFile:path atomically:YES]; 452 | }else if ([content isKindOfClass:[NSDictionary class]]) { 453 | [(NSDictionary *)content writeToFile:path atomically:YES]; 454 | }else if ([content isKindOfClass:[NSJSONSerialization class]]) { 455 | [(NSDictionary *)content writeToFile:path atomically:YES]; 456 | }else if ([content isKindOfClass:[NSMutableString class]]) { 457 | [[((NSString *)content) dataUsingEncoding:NSUTF8StringEncoding] writeToFile:path atomically:YES]; 458 | }else if ([content isKindOfClass:[NSString class]]) { 459 | [[((NSString *)content) dataUsingEncoding:NSUTF8StringEncoding] writeToFile:path atomically:YES]; 460 | }else if ([content isKindOfClass:[UIImage class]]) { 461 | [UIImagePNGRepresentation((UIImage *)content) writeToFile:path atomically:YES]; 462 | }else if ([content conformsToProtocol:@protocol(NSCoding)]) { 463 | [NSKeyedArchiver archiveRootObject:content toFile:path]; 464 | }else { 465 | [NSException raise:@"非法的文件内容" format:@"文件类型%@异常,无法被处理。", NSStringFromClass([content class])]; 466 | 467 | return NO; 468 | } 469 | }else { 470 | return NO; 471 | } 472 | return YES; 473 | } 474 | 475 | #pragma mark - private methods 476 | + (BOOL)isNotError:(NSError **)error { 477 | return ((error == nil) || ((*error) == nil)); 478 | } 479 | 480 | +(NSString *)sizeFormatted:(NSNumber *)size { 481 | double convertedValue = [size doubleValue]; 482 | int multiplyFactor = 0; 483 | 484 | NSArray *tokens = @[@"bytes", @"KB", @"MB", @"GB", @"TB"]; 485 | 486 | while(convertedValue > 1024){ 487 | convertedValue /= 1024; 488 | 489 | multiplyFactor++; 490 | } 491 | 492 | NSString *sizeFormat = ((multiplyFactor > 1) ? @"%4.2f %@" : @"%4.0f %@"); 493 | 494 | return [NSString stringWithFormat:sizeFormat, convertedValue, tokens[multiplyFactor]]; 495 | } 496 | 497 | @end 498 | --------------------------------------------------------------------------------