歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> (iPhone/iPad)計算緩存文件大小

(iPhone/iPad)計算緩存文件大小

日期:2017/3/1 10:36:15   编辑:Linux編程

(iPhone/iPad)計算緩存文件大小:

  1. FileSize.h
  2. //
  3. // FileSize.h
  4. //
  5. //
  6. #import <Cocoa/Cocoa.h>
  7. @interface FileSize : NSObject {
  8. }
  9. // This method converts a given # of bytes into human readable format (KB, MB, GB)
  10. - (NSString *)stringFromFileSize:(int)theSize;
  11. // Returns the size of a file in bytes
  12. - (int)sizeOfFile:(NSString *)path;
  13. // Returns the size of a folder in bytes
  14. - (int)sizeOfFolder:(NSString *)path;
  15. @end
  16. FileSize.m
  17. // FileSize.m
  18. // Created by PCWiz on 30/07/09.
  19. #import "FileSize.h"
  20. @implementation FileSize
  21. - (NSString *)stringFromFileSize:(int)theSize
  22. {
  23. float floatSize = theSize;
  24. if (theSize<1023)
  25. return([NSString stringWithFormat:@"%i bytes",theSize]);
  26. floatSize = floatSize / 1024;
  27. if (floatSize<1023)
  28. return([NSString stringWithFormat:@"%1.1f KB",floatSize]);
  29. floatSize = floatSize / 1024;
  30. if (floatSize<1023)
  31. return([NSString stringWithFormat:@"%1.1f MB",floatSize]);
  32. floatSize = floatSize / 1024;
  33. return([NSString stringWithFormat:@"%1.1f GB",floatSize]);
  34. }
  35. - (int)sizeOfFile:(NSString *)path
  36. {
  37. NSDictionary *fattrib = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES];
  38. int fileSize = (int)[fattrib fileSize];
  39. return fileSize;
  40. }
  41. - (int)sizeOfFolder:(NSString*)folderPath
  42. {
  43. NSArray *contents;
  44. NSEnumerator *enumerator;
  45. NSString *path;
  46. contents = [[NSFileManager defaultManager] subpathsAtPath:folderPath];
  47. enumerator = [contents objectEnumerator];
  48. int fileSizeInt = 0;
  49. while (path = [enumerator nextObject]) {
  50. NSDictionary *fattrib = [[NSFileManager defaultManager] fileAttributesAtPath:[folderPath stringByAppendingPathComponent:path] traverseLink:YES];
  51. fileSizeInt +=[fattrib fileSize];
  52. }
  53. return fileSizeInt;
  54. }
  55. @end
Copyright © Linux教程網 All Rights Reserved