0
我目前正在尝试实现一个小工具,它可以从文件夹内的一组文件创建一个.torrent
。它适用于单个文件。如何用Java中的多个文件创建种子文件?
根据此网站: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);
}
// ...
不知怎的,我不知道如何组装的文件列表。我知道它必须用地图来完成,但我绝望的是我必须选择什么作为关键和价值。
维基百科是有关BitTorrent协议的技术信息的可怕来源。请改用https://wiki.theory.org/BitTorrentSpecification#Metainfo_File_Structure。 – Encombe