项目作者: openzim

项目描述 :
Binding to libzim, read/write ZIM files in Javascript
高级语言: C++
项目地址: git://github.com/openzim/node-libzim.git
创建时间: 2016-06-21T08:36:02Z
项目社区:https://github.com/openzim/node-libzim

开源协议:GNU General Public License v3.0

下载


node-libzim

This is the Node.js binding to the
libzim. Read and write
ZIM files easily in Javascript.

@openzim/libzim"">npm
Build Status
codecov
CodeFactor
License: GPL v3
Join Slack

Dependencies

This package relies on node-addon-api / n-api.

On GNU/Linux & macOS, the package will download a libzim binary. On
other OSes you will need to install libzim separately (see
here
).

Usage

  1. npm i @openzim/libzim

Writing a ZIM file

  1. // write.js
  2. import { Creator, StringItem } from "@openzim/libzim";
  3. (async () => {
  4. console.info('Starting');
  5. const outFile = "./test.zim";
  6. const creator = new Creator()
  7. .configNbWorkers(1)
  8. .configIndexing(true, "en")
  9. .configClusterSize(2048)
  10. .startZimCreation(outFile);
  11. for (let i = 0; i < 100; i++) {
  12. const item = new StringItem(
  13. `file${i}`, // path url
  14. "text/plain", // content-type
  15. `Title ${i}`, // title
  16. {FRONT_ARTICLE: 1, COMPRESS: 1}, // hint option flags
  17. `<h1>Content / Data ${i}</h1>` // content
  18. );
  19. await creator.addItem(item);
  20. }
  21. creator.setMainPath("file0");
  22. await creator.finishZimCreation();
  23. console.log('Done Writing');
  24. })();

Reading a ZIM file

  1. // read.js
  2. import { Archive, SuggestionSearcher, Searcher } from "@openzim/libzim";
  3. (async () => {
  4. const outFile = "./test.zim";
  5. const archive = new Archive(outFile);
  6. console.log(`Archive opened: main entry path - ${archive.mainEntry.path}`);
  7. for (const entry of archive.iterByPath()) {
  8. console.log(`entry: ${entry.path} - ${entry.title}`);
  9. }
  10. const suggestionSearcher = new SuggestionSearcher(archive);
  11. const suggestion = suggestionSearcher.suggest('laborum');
  12. let results = suggestion.getResults(0, 10);
  13. console.log("Suggestion results:");
  14. for(const entry of results) {
  15. console.log(`\t- ${entry.path} - ${entry.title}`);
  16. }
  17. const searcher = new Searcher(archive);
  18. const search = searcher.search(new Query('rem'));
  19. results = search.getResults(0, 10);
  20. console.log("Search results:");
  21. for(const entry of results) {
  22. console.log(`\t- ${entry.path} - ${entry.title}`);
  23. }
  24. const entry = await archive.getEntryByPath("A/laborum");
  25. const item = entry.item;
  26. const blob = item.data;
  27. console.info(`Entry by url (laborum):`, blob.data);
  28. delete archive;
  29. })();

License

GPLv3 or later, see
LICENSE for more details.