Skip to content

Commit

Permalink
exposed public functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pruthvikar committed Jul 18, 2016
1 parent b9924a6 commit 7c246c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
8 changes: 4 additions & 4 deletions Tar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/* Begin PBXBuildFile section */
84C4BA011D3CD28100E07625 /* Tar.h in Headers */ = {isa = PBXBuildFile; fileRef = 84C4BA001D3CD28100E07625 /* Tar.h */; settings = {ATTRIBUTES = (Public, ); }; };
84C4BA081D3CD28100E07625 /* Tar.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 84C4B9FD1D3CD28100E07625 /* Tar.framework */; };
84C4BA191D3CD2AA00E07625 /* NSData+Compression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C4BA171D3CD2AA00E07625 /* NSData+Compression.swift */; };
84C4BA191D3CD2AA00E07625 /* Compression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C4BA171D3CD2AA00E07625 /* Compression.swift */; };
84C4BA1A1D3CD2AA00E07625 /* Tar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84C4BA181D3CD2AA00E07625 /* Tar.swift */; };
84C4BA211D3CD2B800E07625 /* TestData.tar in Resources */ = {isa = PBXBuildFile; fileRef = 84C4BA1B1D3CD2B800E07625 /* TestData.tar */; };
84C4BA221D3CD2B800E07625 /* TestData.lz4 in Resources */ = {isa = PBXBuildFile; fileRef = 84C4BA1C1D3CD2B800E07625 /* TestData.lz4 */; };
Expand Down Expand Up @@ -38,7 +38,7 @@
84C4BA021D3CD28100E07625 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
84C4BA071D3CD28100E07625 /* TarTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TarTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
84C4BA0E1D3CD28100E07625 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
84C4BA171D3CD2AA00E07625 /* NSData+Compression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSData+Compression.swift"; sourceTree = "<group>"; };
84C4BA171D3CD2AA00E07625 /* Compression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Compression.swift; sourceTree = "<group>"; };
84C4BA181D3CD2AA00E07625 /* Tar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tar.swift; sourceTree = "<group>"; };
84C4BA1B1D3CD2B800E07625 /* TestData.tar */ = {isa = PBXFileReference; lastKnownFileType = archive.tar; path = TestData.tar; sourceTree = "<group>"; };
84C4BA1C1D3CD2B800E07625 /* TestData.lz4 */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestData.lz4; sourceTree = "<group>"; };
Expand Down Expand Up @@ -94,7 +94,7 @@
isa = PBXGroup;
children = (
84C4BA001D3CD28100E07625 /* Tar.h */,
84C4BA171D3CD2AA00E07625 /* NSData+Compression.swift */,
84C4BA171D3CD2AA00E07625 /* Compression.swift */,
84C4BA181D3CD2AA00E07625 /* Tar.swift */,
84C4BA021D3CD28100E07625 /* Info.plist */,
);
Expand Down Expand Up @@ -232,7 +232,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
84C4BA191D3CD2AA00E07625 /* NSData+Compression.swift in Sources */,
84C4BA191D3CD2AA00E07625 /* Compression.swift in Sources */,
84C4BA1A1D3CD2AA00E07625 /* Tar.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
File renamed without changes.
48 changes: 25 additions & 23 deletions Tar/Tar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension String {
}
}

struct Tar {
public struct Tar {

static func compress(inPath: String, outPath: String, using: NSData.Algorithm) {
let dataToCompress = Tar.tar(inPath)
Expand Down Expand Up @@ -93,7 +93,7 @@ struct Tar {



static func untar(data: NSData, toPath: String) {
public static func untar(data: NSData, toPath: String) {
let fileManager = NSFileManager.defaultManager()
try! fileManager.createDirectoryAtPath(toPath, withIntermediateDirectories: true, attributes: nil)

Expand Down Expand Up @@ -121,6 +121,29 @@ struct Tar {

}

public static func tar(path: String) -> NSData {

let fm = NSFileManager.defaultManager()
let md = NSMutableData()
if fm.fileExistsAtPath(path) {

for filePath in fm.enumeratorAtPath(path)! {
var isDir: ObjCBool = ObjCBool(false)

fm.fileExistsAtPath(path.stringByAppendingPathComponent(filePath as! String), isDirectory: &isDir)
let tarContent = binaryEncodeData(filePath as! String, inDirectory: path, isDirectory: Bool(isDir))
md.appendData(tarContent)
}
var block = [UInt8](count: TAR_BLOCK_SIZE * 2, repeatedValue: UInt8())
memset(&block, Int32(NullChar), TAR_BLOCK_SIZE * 2)
md.appendData(NSData(bytes: UnsafePointer<UInt8>(block), length: block.count))
return md as NSData
}

return NSData()
}



private static func writeFileDataFor(data: NSData, atLocation: UInt64, withLength: Int, atPath: String) {
NSFileManager.defaultManager().createFileAtPath(atPath, contents: data.subdataWithRange(NSRange(location: Int(atLocation), length: Int(withLength))), attributes: nil)
Expand All @@ -144,27 +167,6 @@ struct Tar {
return strtol(sizeString, nil, 8)
}

static func tar(path: String) -> NSData {

let fm = NSFileManager.defaultManager()
let md = NSMutableData()
if fm.fileExistsAtPath(path) {

for filePath in fm.enumeratorAtPath(path)! {
var isDir: ObjCBool = ObjCBool(false)

fm.fileExistsAtPath(path.stringByAppendingPathComponent(filePath as! String), isDirectory: &isDir)
let tarContent = binaryEncodeData(filePath as! String, inDirectory: path, isDirectory: Bool(isDir))
md.appendData(tarContent)
}
var block = [UInt8](count: TAR_BLOCK_SIZE * 2, repeatedValue: UInt8())
memset(&block, Int32(NullChar), TAR_BLOCK_SIZE * 2)
md.appendData(NSData(bytes: UnsafePointer<UInt8>(block), length: block.count))
return md as NSData
}

return NSData()
}

private static func binaryEncodeData(forPath: String, inDirectory: String, isDirectory: Bool) -> NSData {

Expand Down

0 comments on commit 7c246c1

Please sign in to comment.