title: Java: How to create torrent file for multiple files or directories?
body: <p>The next code is a part of jBitTorrent API for Java a bit modified by me:
  <code><pre>
              if (!torr.saveAs.matches(""))
                  info.put("name", torr.saveAs);
              else
                  info.put("name", "noDirSpec");
              ArrayList files = new ArrayList();
              for (int i = 0; i &lt; 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);</p>
  
  <pre><code>            if(f.isFile())
              {
                  pathList.add(path[path.length - 1]);
              }
              else if(f.isDirectory())
              {
  </code></pre>
  
  <p><b>//                    ???</b>
                  }
  </pre></code>
  This is torrent file(s) generation. So the «String[] path» is commented because of «\\» doesn't match linux slashes and generate invalid paths. Specification for multiple files is:</p>
  
  <blockquote>
  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. 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. This is not used by BitTorrent at all, but it is included by some programs for greater compatibility.
  path: a list containing one or more string elements that together represent the path and filename. 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". This is encoded as a bencoded list of strings such as l4:dir14:dir28:file.exte
  </blockquote>
  
  <p>And for single file is:</p>
  
  <blockquote>
  For the case of the single-file mode, the info dictionary contains the following structure:
  name: the filename. 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. This is not used by BitTorrent at all, but it is included by some programs for greater compatibility.
  </blockquote>
  
  <p>So if I will use the previous code for single file here will be generated this torrent:
  <img src="https://i.stack.imgur.com/CthyL.png" alt="enter image description here"></p>
  
  <p>And torrent files contains:</p>
  
  <blockquote>
  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
  </blockquote>
  
  <p>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. But I need something like this:
  <img src="https://i.stack.imgur.com/ssZvE.png" alt="enter image description here">
  And the valid torrent file is:</p>
  
  <blockquote>
  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�I��oQ�CI�a1R�{ۦ��
  </blockquote>