Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 534361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:36:15+00:00 2026-05-13T09:36:15+00:00

I want to generate a .torrent file in Java, but I don’t want a

  • 0

I want to generate a .torrent file in Java, but I don’t want a big API that does anything like scraping trackers, seeding, etc. This is just for a client that generates meta data. What lightweight solutions exist? I am only generating a .torrent of a single .zip file.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:36:16+00:00Added an answer on May 13, 2026 at 9:36 am

    I have put together this self-contained piece of Java code to prepare a .torrent file with a single file.

    The .torrent file is created by calling createTorrent() passing the name of the .torrent file, the name of the shared file and the tracker URL.

    createTorrent() uses hashPieces() to hash the file pieces using Java’s MessageDigest class. Then createTorrent() prepares a meta info dictionary containing the torrent meta-data. This dictionary is then serialized in the proper bencode format using the encode*() methods and saved in a .torrent file.

    See the BitTorrent spec for details.

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.SortedMap;
    import java.util.TreeMap;
    
    public class Torrent {
        private static void encodeObject(Object o, OutputStream out) throws IOException {
            if (o instanceof String)
                encodeString((String) o, out);
            else if (o instanceof Map)
                encodeMap((Map) o, out);
            else if (o instanceof byte[])
                encodeBytes((byte[]) o, out);
            else if (o instanceof Number)
                encodeLong(((Number) o).longValue(), out);
            else
                throw new Error("Unencodable type");
        }
    
        private static void encodeLong(long value, OutputStream out) throws IOException {
            out.write('i');
            out.write(Long.toString(value).getBytes("US-ASCII"));
            out.write('e');
        }
    
        private static void encodeBytes(byte[] bytes, OutputStream out) throws IOException {
            out.write(Integer.toString(bytes.length).getBytes("US-ASCII"));
            out.write(':');
            out.write(bytes);
        }
    
        private static void encodeString(String str, OutputStream out) throws IOException {
            encodeBytes(str.getBytes("UTF-8"), out);
        }
    
        private static void encodeMap(Map<String, Object> map, OutputStream out) throws IOException {
            // Sort the map. A generic encoder should sort by key bytes
            SortedMap<String, Object> sortedMap = new TreeMap<String, Object>(map);
            out.write('d');
            for (Map.Entry<String, Object> e : sortedMap.entrySet()) {
                encodeString(e.getKey(), out);
                encodeObject(e.getValue(), out);
            }
            out.write('e');
        }
    
        private static byte[] hashPieces(File file, int pieceLength) throws IOException {
            MessageDigest sha1;
            try {
                sha1 = MessageDigest.getInstance("SHA");
            } catch (NoSuchAlgorithmException e) {
                throw new Error("SHA1 not supported");
            }
            InputStream in = new FileInputStream(file);
            ByteArrayOutputStream pieces = new ByteArrayOutputStream();
            byte[] bytes = new byte[pieceLength];
            int pieceByteCount = 0, readCount = in.read(bytes, 0, pieceLength);
            while (readCount != -1) {
                pieceByteCount += readCount;
                sha1.update(bytes, 0, readCount);
                if (pieceByteCount == pieceLength) {
                    pieceByteCount = 0;
                    pieces.write(sha1.digest());
                }
                readCount = in.read(bytes, 0, pieceLength - pieceByteCount);
            }
            in.close();
            if (pieceByteCount > 0)
                pieces.write(sha1.digest());
            return pieces.toByteArray();
        }
    
        public static void createTorrent(File file, File sharedFile, String announceURL) throws IOException {
            final int pieceLength = 512 * 1024;
            Map<String, Object> info = new HashMap<>();
            info.put("name", sharedFile.getName());
            info.put("length", sharedFile.length());
            info.put("piece length", pieceLength);
            info.put("pieces", hashPieces(sharedFile, pieceLength));
            Map<String, Object> metainfo = new HashMap<String, Object>();
            metainfo.put("announce", announceURL);
            metainfo.put("info", info);
            OutputStream out = new FileOutputStream(file);
            encodeMap(metainfo, out);
            out.close();
        }
    
        public static void main(String[] args) throws Exception {
            createTorrent(new File("C:/x.torrent"), new File("C:/file"), "http://example.com/announce");
        }
    }
    

    Code edits: Make this a bit more compact, fix methods visibility, use character literals where appropriate, use instanceof Number. And more recently read the file using block I/O because I ‘m trying to use it for real and byte I/O is just slow,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.