 checkDirExists(atPath: sourceDir)
 clearDirectory(atPath: sourceDir)
 
-let textDir = sourceDir.appendingPathComponent("text")
-let imgDir = sourceDir.appendingPathComponent("images")
+let resultDir = URL(fileURLWithPath: fm.currentDirectoryPath).appendingPathComponent("result", isDirectory: true)
+
+checkDirExists(atPath: resultDir)
+clearDirectory(atPath: resultDir)
+
+let textDir = resultDir.appendingPathComponent("text")
+let imgDir = resultDir.appendingPathComponent("images")
 
 checkDirExists(atPath: textDir)
 checkDirExists(atPath: imgDir)

    let currentURL = URL(string: address.replacingCharacters(in: address.range(of: "{#0}")!, with: "\(i)"))!
    dump("loading: \(currentURL)")
    let data = try! Data(contentsOf: currentURL)
    let savePath = sourceDir.appendingPathComponent("text", isDirectory: true).appendingPathComponent(currentURL.lastPathComponent)
    dump("saving to: \(savePath)")
    try! data.write(to: savePath)
}

    let currentImgURL = URL(string: imgAddress.replacingCharacters(in: imgAddress.range(of: "{#0}")!, with: "\(i)"))!
    dump("loading img: \(currentImgURL)")
    let data = try! Data(contentsOf: currentImgURL)
    let savePath = imgDir.appendingPathComponent(currentImgURL.lastPathComponent)
    dump("saving to: \(savePath)")
    try! data.write(to: savePath)
}

let metaInfDirPath = resultDir.appendingPathComponent("META-INF")
dump("creating directory \(metaInfDirPath)")
try! fm.createDirectory(at: metaInfDirPath, withIntermediateDirectories: false, attributes: nil)
let metaInfFilePath = metaInfDirPath.appendingPathComponent("container.xml")
dump("creating file \(metaInfFilePath)")
try! containerXMLData().write(to: metaInfFilePath)
let textContents = try! fm.contentsOfDirectory(at: textDir, includingPropertiesForKeys: nil, options: [])
let imgContents = try! fm.contentsOfDirectory(at: imgDir, includingPropertiesForKeys: nil, options: [])
let packageFilePath = resultDir.appendingPathComponent("content.opf")
dump("creating file \(packageFilePath)")
try! packageXMLData(withFileList: textContents.map { $0.lastPathComponent } + imgContents.map {$0.lastPathComponent}).write(to: packageFilePath)
