summaryrefslogtreecommitdiff
path: root/source/org/openzim/ZIMTest.java
blob: 92ed96dac152994bc3452ce2f9a7369d228f1c1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright (C) 2011 Arunesh Mathur
 *
 * This file is a part of zimreader-java.
 *
 * zimreader-java is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3.0 as
 * published by the Free Software Foundation.
 *
 * zimreader-java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with zimreader-java.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openzim;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

import org.openzim.ZIMReader.DirectoryEntry;

public class ZIMTest {

    public static void main(final String[] args) {
        if(args.length!=1) {
            System.out.println("Usage: java ZIMTest <ZIM_FILE>");
            System.exit(0);
        }

        try {
            // args[0] is the Zim File's location
            final ZIMFile file = new ZIMFile(args[0]);

            // Associate the Zim File with a Reader
            final ZIMReader zReader = new ZIMReader(file);

            // print a list of urls and titles
            int c = Math.min(10, file.header_entryCount);
            for (int i = 0; i < c; i++) {
                System.out.println("URL by URL   " + i + ": " + zReader.getURLByURLOrder(i));
                System.out.println("URL by Title " + i + ": " + zReader.getURLByTitleOrder(i));
                DirectoryEntry entry = zReader.getDirectoryInfo(i);
                System.out.println("URL   by Pos " + i + ": " + entry.url);
                System.out.println("Title by Pos " + i + ": " + entry.title);
                System.out.println("Namespace by Pos " + i + ": " + entry.namespace);
            }

            // print article c-1
            DirectoryEntry directory_entry = zReader.getDirectoryInfo(c - 1);
            byte[] articleBytes = zReader.getArticleData(directory_entry);
            String article = articleBytes == null ? "NULL" : new String(articleBytes, StandardCharsets.UTF_8);
            System.out.println(article);

            // iterate over all entries
            Iterator<ZIMReader.ArticleBlobEntry> i = zReader.new ClusterIterator();
            int count = 0;
            while (i.hasNext()) {
                ZIMReader.ArticleBlobEntry entry = i.next();
                System.out.println(entry.article.url);
                count++;
            }
            System.out.println("Number of articles extracted: " + count);
            System.out.println("Number of articles expected:  " + file.header_entryCount);
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }

}