[英]How to create a torrent file with multiple files in Java?
我目前正在尝试实现一个小型工具,该工具可以从文件夹内的一组文件中创建一个.torrent
。 对于单个文件,它工作得很好。
根据此站点: Torrent_file多个文件存储在一个文件集中。
到目前为止,这是我所做的:
public Map<Object, Object> getFiles(File dict) throws IOException, NoSuchAlgorithmException {
Map<Object, Object> files = new HashMap<Object, Object>();
FileOutputStream fos = new FileOutputStream(merged);
for (File fileEntry : dict.listFiles())
{
if (fileEntry.isFile())
{
Map<String, Object> file = new HashMap<String, Object>();
file.put("path", fileEntry.getName());
file.put("length", fileEntry.length());
FileInputStream fis = new FileInputStream(fileEntry);
byte[] byteArray = new byte[(int) fileEntry.length()];
fis.read(byteArray, 0, (int) fileEntry.length());
fos.write(byteArray);
fos.flush();
files.put(file.get("path"), file.get("length"));
}
}
fos.close();
pieces = hashPieces(merged, pieceLength);
return files;
}
我尝试为每个单独的文件创建一个映射,然后将这些映射放入包含所有文件的另一个映射中。 然后,我将文件作为文件数组合并到一个大文件中,以计算碎片哈希。 但是,文件结构部分无法正常工作。
多个文件的方法由以下方式调用:
// ...
Map<String, Object> info = new HashMap<String, Object>();
info.put("name", sharedFile.getName());
if (sharedFile.isDirectory()) {
Map<Object, Object> path = getFiles(sharedFile);
info.put("files", path);
}
// ...
不知何故我不知道如何汇编文件列表。 我知道必须使用地图来完成,但是我对于必须选择什么作为键和值感到绝望。
信息映射中files
键下的值不是映射,而是映射列表,每个映射都描述一个文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.