     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 = sourceDir.appendingPathComponent("images", isDirectory: true).appendingPathComponent(currentImgURL.lastPathComponent)
+    let savePath = imgDir.appendingPathComponent(currentImgURL.lastPathComponent)
     dump("saving to: \(savePath)")
     try! data.write(to: savePath)
 }
 
-let resultDir = URL(fileURLWithPath: fm.currentDirectoryPath).appendingPathComponent("result", isDirectory: true)
-checkDirExists(atPath: resultDir)
-clearDirectory(atPath: resultDir)
-
 let metaInfDirPath = resultDir.appendingPathComponent("META-INF")
 dump("creating directory \(metaInfDirPath)")
 try! fm.createDirectory(at: metaInfDirPath, withIntermediateDirectories: false, attributes: nil)
-let metaInfFilePath = resultDir.appendingPathComponent("container.xml")
+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)

    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 version = XMLElement(kind: .attribute)
    version.name = "version"
    version.stringValue = "1.0"
    package.addAttribute(version)
    
    let metadata = XMLElement(name: "metadata")
