简体   繁体   English

Java:如何为多个文件或目录创建种子文件?

[英]Java: How to create torrent file for multiple files or directories?

The next code is a part of jBitTorrent API for Java a bit modified by me: 下一个代码是Java的jBitTorrent API的一部分,我对此做了一些修改:


            if (!torr.saveAs.matches(""))
                info.put("name", torr.saveAs);
            else
                info.put("name", "noDirSpec");
            ArrayList files = new ArrayList();
            for (int i = 0; i < torr.name.size(); i++) {
                SortedMap file = new TreeMap();
                file.put("length", (Integer) torr.length.get(i));
//                String[] path = ((String) torr.name.get(i)).split("\\");
                String[] path = ((String) torr.name.get(i)).split("/");
                File f = new File((String)(torr.name.get(i)));
                ArrayList pathList = new ArrayList(path.length);

            if(f.isFile())
            {
                pathList.add(path[path.length - 1]);
            }
            else if(f.isDirectory())
            {

// ??? }

This is torrent file(s) generation. 这是种子文件生成。 So the «String[] path» is commented because of «\\\\» doesn't match linux slashes and generate invalid paths. 因此,对«String [] path»进行了注释,因为«\\\\»与Linux斜杠不匹配并生成了无效的路径。 Specification for multiple files is: 多个文件的规范是:

For the case of the multi-file mode, the info dictionary contains the following structure: name: the filename of the directory in which to store all the files. 对于多文件模式,信息字典包含以下结构:name:存储所有文件的目录的文件名。 This is purely advisory. 这纯粹是建议。 (string) files: a list of dictionaries, one for each file. (字符串)文件:词典列表,每个文件一个。 Each dictionary in this list contains the following keys: length: length of the file in bytes (integer) md5sum: (optional) a 32-character hexadecimal string corresponding to the MD5 sum of the file. 此列表中的每个词典都包含以下键:length:文件的长度(以字节为单位)(整数)md5sum :(可选)与文件的MD5总和相对应的32个字符的十六进制字符串。 This is not used by BitTorrent at all, but it is included by some programs for greater compatibility. BitTorrent完全不使用此功能,但是某些程序将其包含在内,以实现更大的兼容性。 path: a list containing one or more string elements that together represent the path and filename. path:包含一个或多个字符串元素的列表,这些字符串元素一起代表路径和文件名。 Each element in the list corresponds to either a directory name or (in the case of the final element) the filename. 列表中的每个元素都对应于目录名称或文件名(对于最后一个元素而言)。 For example, a the file "dir1/dir2/file.ext" would consist of three string elements: "dir1", "dir2", and "file.ext". 例如,文件“ dir1 / dir2 / file.ext”将由三个字符串元素组成:“ dir1”,“ dir2”和“ file.ext”。 This is encoded as a bencoded list of strings such as l4:dir14:dir28:file.exte 这被编码为字符串的bencoded列表,例如l4:dir14:dir28:file.exte

And for single file is: 对于单个文件是:

For the case of the single-file mode, the info dictionary contains the following structure: name: the filename. 对于单文件模式,信息字典包含以下结构:name:文件名。 This is purely advisory. 这纯粹是建议。 (string) length: length of the file in bytes (integer) md5sum: (optional) a 32-character hexadecimal string corresponding to the MD5 sum of the file. (字符串)长度:文件的长度(字节)(整数)md5sum :(可选)与文件的MD5总和相对应的32个字符的十六进制字符串。 This is not used by BitTorrent at all, but it is included by some programs for greater compatibility. BitTorrent完全不使用此功能,但是某些程序将其包含在内以提高兼容性。

So if I will use the previous code for single file here will be generated this torrent: 因此,如果我将单个文件使用先前的代码,则会在这里生成以下种子文件: 在此处输入图片说明

And torrent files contains: 种子文件包含:

d8:announce0:10:created by18:jBittorrentAPI 1.04:infod6:lengthi1645522e4:name27:ppkBB3cker-free-20101212.7z12:piece lengthi102400e6:pieces340:(ѕн'xІZUЙ ЃКХЪйкжѕЫ››3УМ‹ЁWпЖћA‡ ѕ,т C;НлАтжѓњЧ0x‡Е %РъEЬ™сЖ®6ЦоёSЌ1пж`.бй%»†Ґ0VЩ%“ѕђoђџ'hЃ7ЦЭjъ\\!RОQ.lВЦрjяGЋХ®^zЏ ЎЉSЖЕ|„1D”кCРк+бћJLХmЛF`Ы"ГљSЪv$Л Оuњi€ЙxQee d8:announce0:10:创建者18:jBittorrentAPI 1.04:infod6:lengthi1645522e4:name27:ppkBB3cker-free-20101212.7z12:piece lengthi102400e6:pieces340:(sн'xІZUЙЃКХЪйкжsx›› РъEЬ™сЖ®6ЦоёSЌ1пж`.бй%»†Ґ0VЩ%”sђoђџ'hЃ7ЦЭjъ\\!RОQ.lВЦрjяGЋХ®^zЏSЎЉЕ|„ 1D“кCРк+бћJLХmЛF`Ы”љЫQљx$О

Okay. 好的。 But how I should generate torrent for directory? 但是我应该如何为目录生成种子? The problem is that in unmodified API will be generated files with full path from root. 问题在于,在未经修改的API中,将生成具有root用户完整路径的文件。 But I need something like this: 但是我需要这样的东西: 在此处输入图片说明 And the valid torrent file is: 有效的种子文件是:

d10:created by25:Transmission/2.05 (11488)13:creation datei1302559094e8:encoding5:UTF-84:infod5:filesld6:lengthi196126e4:pathl29:1231190375_bulldozer.jar.parteed6:lengthi937518e4:pathl13:FIFA_2009-RPL35:240x320-fifa-rpl-2009(eng).jar.parteed6:lengthi802555e4:pathl13:FIFA_2009-RPL31:fifa-09-rpl_176x220_en.jar.parteed6:lengthi985098e4:pathl13:FIFA_2009-RPL36:FIFA_2009-RPL_240x320_(rus).jar.parteed6:lengthi138534e4:pathl41:GAZilaMarshrutkaUbiyca[game-torrent.info]41:GAZila_Marshrutka_Ubiyca_176x208.jar.parteed6:lengthi175851e4:pathl41:GAZilaMarshrutkaUbiyca[game-torrent.info]41:GAZila_Marshrutka_Ubiyca_240x320.jar.parteed6:lengthi827473e4:pathl33:God of War 3D (240х320).jar.parteed6:lengthi295164e4:pathl26:Men_In_Black-Alien_Assault39:Men_In_Black-Alien_Assault_176.jar.parteed6:lengthi374046e4:pathl26:Men_In_Black-Alien_Assault39:Men_In_Black-Alien_Assault_240.jar.parteee4:name53:5 отличных игр для мобильника12:piece lengthi32768e6:pieces2900: dSI %|Q §I d10:创建者25:传输/2.05(11488)13:创建日期i1302559094e8:encoding5:UTF-84:infod5:filesld6:lengthi196126e4:pathl29:1231190375_bulldozer.jar.parteed6:lengthi937518e4:pathl13:FIFA_2009-RPL35:240 2009(ENG).jar.parteed6:lengthi802555e4:pathl13:FIFA_2009-RPL31:fifa-09-rpl_176x220_en.jar.parteed6:lengthi985098e4:pathl13:FIFA_2009-RPL36:FIFA_2009-RPL_240x320_(rus).jar.parteed6:lengthi41:41 GAZilaMarshrutkaUbiyca [game-torrent.info] 41:GAZila_Marshrutka_Ubiyca_176x208.jar.parteed6:lengthi175851e4:pathl41:GAZilaMarshrutkaUbiyca [game-torrent.info] 41:GAZila_Marshrutka_Ubiyca_length:3.240:320.240.320 parteed6:lengthi295164e4:pathl26:Men_In_Black-Alien_Assault39:Men_In_Black-Alien_Assault_176.jar.parteed6:lengthi374046e4:pathl26:Men_In_Black-Alien_Assault39:Men_In_Black-Alien_Assault_240.jar.parteee4:name53:5отличныхигрдлямобильника12:件lengthi32768e6:pieces2900:DSI %|Q §I。 I oQ CI a1R {ۦ I oQ CI a1R {ۦ

I recommend you use System.getProperty("file.separator") to create valid paths on all OS's instead of hardcoding it to the windows file separator (\\) 我建议您使用System.getProperty("file.separator")在所有操作系统上创建有效路径,而不是将其硬编码为Windows文件分隔符(\\)

I havent used this API, but looking at the following line.. 我还没有使用过此API,但请看以下一行。

For the case of the multi-file mode, the info dictionary contains the following structure: name: the filename of the directory in which to store all the files. 对于多文件模式,信息字典包含以下结构:name:存储所有文件的目录的文件名。

I'm guessing you could use 我猜你可以用

pathList.add(path[path.length - 1]);

irrespective if the File object is a file or a directory. 不管File对象是文件还是目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM