npak-archiver 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -2
- package/api.js +7 -0
- package/cjk.js +20 -0
- package/codes.js +75 -0
- package/emoji_codes.js +34 -0
- package/errors.js +29 -0
- package/index.js +118 -0
- package/npak-icon.png +0 -0
- package/pack.js +231 -0
- package/package.json +29 -4
- package/unpack.js +148 -0
package/README.md
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 📦 npak (Node Pack Archiver)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Быстрый кроссплатформенный бинарный архиватор на Node.js с собственным закрытым форматом контейнера **`.npak`**.
|
|
4
|
+
|
|
5
|
+
## ✨ Основные возможности
|
|
6
|
+
|
|
7
|
+
- **Собственный бинарный формат `.npak`**: двухступенчатое кодирование через закрытые частотные словари и 4-битные спецкоманды.
|
|
8
|
+
- **Полная поддержка Unicode**: приоритетное кодирование кириллицы, латиницы, символов кода, эмодзи и иероглифов CJK.
|
|
9
|
+
- **Гибридный движок сжатия**: текстовые файлы сжимаются по частотному словарю, а медиафайлы и бинарники упаковываются напрямую без раздувания.
|
|
10
|
+
- **Потоковая обработка**: запись на диск через буфер без переполнения оперативной памяти даже на больших папках.
|
|
11
|
+
- **Защита целостности**: фирменная сигнатура `NPAK` и проверка контрольной суммы `SHA-256` при распаковке.
|
|
12
|
+
- **Интеграция с Linux**: автоматическая установка фирменной иконки и распаковка архивов по двойному клику мышкой.
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## 🚀 Установка
|
|
17
|
+
|
|
18
|
+
### Глобальная установка через npm
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install -g npak
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Установка из исходников (с настройкой иконки и двойного клика в Linux)
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
git clone https://github.com
|
|
28
|
+
cd npak
|
|
29
|
+
sudo node setup.js
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
_(Для Windows достаточно выполнить `node setup.js` без `sudo`)._
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## 💻 Использование в терминале
|
|
37
|
+
|
|
38
|
+
### Упаковка папки или файла
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npak pack ./my_project ./archive.npak
|
|
42
|
+
# или короткий псевдоним:
|
|
43
|
+
npak p ./my_project ./archive.npak
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Распаковка архива
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npak unpack ./archive.npak ./restored_folder
|
|
50
|
+
# или короткий псевдоним:
|
|
51
|
+
npak u ./archive.npak ./restored_folder
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Работа со стандартным форматом `.zip`
|
|
55
|
+
|
|
56
|
+
Утилита поддерживает прозрачное делегирование задач системному архиватору для работы с `.zip`:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npak pack ./my_project ./archive.zip -f zip
|
|
60
|
+
npak unpack ./archive.zip ./restored_folder -f zip
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
---
|
|
64
|
+
|
|
65
|
+
## 🧩 Использование в коде (Node.js API)
|
|
66
|
+
|
|
67
|
+
Вы можете подключать `npak` как обычную библиотеку в своих проектах:
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
const { pack, unpack } = require("npak");
|
|
71
|
+
|
|
72
|
+
// Упаковать папку или файл
|
|
73
|
+
pack("./source_folder", "./backup.npak");
|
|
74
|
+
|
|
75
|
+
// Распаковать архив
|
|
76
|
+
unpack("./backup.npak", "./output_folder");
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## 🛠 Логирование ошибок
|
|
82
|
+
|
|
83
|
+
При возникновении проблем (отсутствие файла, нарушение контрольной суммы `SHA-256`, повреждение структуры) утилита выводит понятную причину на русском языке в консоль и сохраняет историю в файл `~/.npak-error.log`.
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## 📄 Лицензия
|
|
88
|
+
|
|
89
|
+
MIT
|
package/api.js
ADDED
package/cjk.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const CJK_RANGES = [
|
|
2
|
+
[0x3000, 0x303f],
|
|
3
|
+
[0x3040, 0x309f],
|
|
4
|
+
[0x30a0, 0x30ff],
|
|
5
|
+
[0x4e00, 0x9fff],
|
|
6
|
+
[0xac00, 0xd7af],
|
|
7
|
+
[0x3400, 0x4dbf],
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
function getCjkChars() {
|
|
11
|
+
const chars = [];
|
|
12
|
+
for (const [start, end] of CJK_RANGES) {
|
|
13
|
+
for (let cp = start; cp <= end; cp++) {
|
|
14
|
+
chars.push(String.fromCodePoint(cp));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return chars;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { getCjkChars };
|
package/codes.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const TIER_3_POPULAR_EMOJI = require(path.join(__dirname, "emoji_codes.js"));
|
|
3
|
+
const { getCjkChars } = require(path.join(__dirname, "cjk.js"));
|
|
4
|
+
const NIBBLE_MAP = {
|
|
5
|
+
0: 0b0000,
|
|
6
|
+
1: 0b0001,
|
|
7
|
+
2: 0b0010,
|
|
8
|
+
3: 0b0011,
|
|
9
|
+
4: 0b0100,
|
|
10
|
+
5: 0b0101,
|
|
11
|
+
6: 0b0110,
|
|
12
|
+
7: 0b0111,
|
|
13
|
+
8: 0b1000,
|
|
14
|
+
9: 0b1001,
|
|
15
|
+
SEP: 0b1010,
|
|
16
|
+
CMD_DIR: 0b1011,
|
|
17
|
+
CMD_FILE: 0b1100,
|
|
18
|
+
CMD_DATA: 0b1101,
|
|
19
|
+
CMD_END: 0b1110,
|
|
20
|
+
CMD_BIN: 0b1111,
|
|
21
|
+
};
|
|
22
|
+
const REV_NIBBLE_MAP = Object.fromEntries(
|
|
23
|
+
Object.entries(NIBBLE_MAP).map(([k, v]) => [v, k]),
|
|
24
|
+
);
|
|
25
|
+
const TIER_1_ULTRA_COMMON =
|
|
26
|
+
" \n\r\t" +
|
|
27
|
+
"оеаинтсрвлкмдпуяыьгзбчйхжшюцщэфъё" +
|
|
28
|
+
"etaoinshrdlcumwfgypbvkjxqz" +
|
|
29
|
+
".,!?-_:;()[]{}\"'/\\+=*<>@#$%^&|~`" +
|
|
30
|
+
"0123456789" +
|
|
31
|
+
"ОЕАИНТСРВЛКМДПУЯЫЬГЗБЧЙХЖШЮЦЩЭФЪЁ" +
|
|
32
|
+
"ETAOINSHRDLCUMWFGYPBVKJXQZ";
|
|
33
|
+
const TIER_2_COMMON_SYMBOLS =
|
|
34
|
+
"«»—–…№₽$€£¥©®™°±×÷≈≠≤≥∞√∑∏∫∂∆πµΩ" +
|
|
35
|
+
"→←↑↓↔⇒⇔•·▪▫●○■□▲△▼▽◆◇★☆✓✔✕✖✗✘" +
|
|
36
|
+
"ўЎіІїЇєЄґҐ" +
|
|
37
|
+
"äöüßÄÖÜéèêëàâîïôùûçÉÈÊËÀÂÎÏÔÙÛÇáíóúñÁÍÓÚÑ";
|
|
38
|
+
const CHAR_TO_NUM = new Map();
|
|
39
|
+
const NUM_TO_CHAR = [];
|
|
40
|
+
function registerChar(char) {
|
|
41
|
+
if (!CHAR_TO_NUM.has(char)) {
|
|
42
|
+
const code = NUM_TO_CHAR.length;
|
|
43
|
+
CHAR_TO_NUM.set(char, code);
|
|
44
|
+
NUM_TO_CHAR.push(char);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
for (const char of TIER_1_ULTRA_COMMON) registerChar(char);
|
|
48
|
+
for (const char of TIER_2_COMMON_SYMBOLS) registerChar(char);
|
|
49
|
+
for (const char of TIER_3_POPULAR_EMOJI) registerChar(char);
|
|
50
|
+
for (const char of getCjkChars()) registerChar(char);
|
|
51
|
+
for (let cp = 0x0020; cp <= 0x323af; cp++) {
|
|
52
|
+
if (cp >= 0xd800 && cp <= 0xdfff) continue;
|
|
53
|
+
if (cp >= 0xe000 && cp <= 0xf8ff) continue;
|
|
54
|
+
if (cp >= 0xf0000) continue;
|
|
55
|
+
registerChar(String.fromCodePoint(cp));
|
|
56
|
+
}
|
|
57
|
+
function charToCode(char) {
|
|
58
|
+
const code = CHAR_TO_NUM.get(char);
|
|
59
|
+
if (code !== undefined) return code;
|
|
60
|
+
return NUM_TO_CHAR.length + char.codePointAt(0);
|
|
61
|
+
}
|
|
62
|
+
function codeToChar(num) {
|
|
63
|
+
if (num < NUM_TO_CHAR.length) {
|
|
64
|
+
return NUM_TO_CHAR[num];
|
|
65
|
+
}
|
|
66
|
+
return String.fromCodePoint(num - NUM_TO_CHAR.length);
|
|
67
|
+
}
|
|
68
|
+
module.exports = {
|
|
69
|
+
NIBBLE_MAP,
|
|
70
|
+
REV_NIBBLE_MAP,
|
|
71
|
+
CHAR_TO_NUM,
|
|
72
|
+
NUM_TO_CHAR,
|
|
73
|
+
charToCode,
|
|
74
|
+
codeToChar,
|
|
75
|
+
};
|
package/emoji_codes.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const emoji =
|
|
2
|
+
"\u200D\uFE0F\uFE0E" +
|
|
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
|
+
"〽️✳️✴️❇️©️®️™️#️⃣*️⃣0️⃣1️⃣2️⃣3️⃣4️⃣5️⃣6️⃣7️⃣8️⃣9️⃣🔟🔠🔡🔢🔣🔤🅰️🆎🅱️🆑🆒🆓ℹ️🆔Ⓜ️🆕🆖🅾️🆗🅿️🆘🆙🆚🈁🈂️🈷️🈶🈯🉐🈹🈚🈲🉑🈸🈴🈳㊗️㊙️🈺🈵🔴🟠🟡🟢🔵🟣🟤" +
|
|
28
|
+
"⚫⚪🟥🟧🟨🟩🟦🟪🟫⬛⬜◼️◻️◾◽▪️▫️🔶🔷🔸🔹🔺🔻💠🔘🔳🔲💬👁️🗨️🗨️🗯️💭👨👩👦👨👩👧👨👩👧👦👨👩👦👦👨👩👧👧👨👦👨👦👦👨👧👨👧👦👨👧👧👩👦👩👦👦👩👧👩👧👦👩👧👧👪🧑🧑🧒🧑🧑🧒🧒🧑🧒🧑🧒🧒🏁🚩🎌🏴🏳️🇦🇨🇦🇩🇦🇪🇦🇫🇦🇬🇦🇮🇦🇱🇦🇲🇦🇴🇦🇶🇦🇷🇦🇸🇦🇹" +
|
|
29
|
+
"🇦🇺🇦🇼🇦🇽🇦🇿🇧🇦🇧🇧🇧🇩🇧🇪🇧🇫🇧🇬🇧🇭🇧🇮🇧🇯🇧🇱🇧🇲🇧🇳🇧🇴🇧🇶🇧🇷🇧🇸🇧🇹🇧🇻🇧🇼🇧🇾🇧🇿🇨🇦🇨🇨🇨🇩🇨🇫🇨🇬🇨🇭🇨🇮🇨🇰🇨🇱🇨🇲🇨🇳🇨🇴🇨🇵🇨🇶🇨🇷🇨🇺🇨🇻🇨🇼🇨🇽🇨🇾🇨🇿🇩🇪🇩🇬🇩🇯🇩🇰🇩🇲🇩🇴🇩🇿🇪🇦🇪🇨🇪🇪🇪🇬🇪🇭🇪🇷🇪🇸🇪🇹🇪🇺🇫🇮🇫🇯🇫🇰🇫🇲🇫🇴🇫🇷🇬🇦" +
|
|
30
|
+
"🇬🇧🇬🇩🇬🇪🇬🇫🇬🇬🇬🇭🇬🇮🇬🇱🇬🇲🇬🇳🇬🇵🇬🇶🇬🇷🇬🇸🇬🇹🇬🇺🇬🇼🇬🇾🇭🇰🇭🇲🇭🇳🇭🇷🇭🇹🇭🇺🇮🇨🇮🇩🇮🇪🇮🇱🇮🇲🇮🇳🇮🇴🇮🇶🇮🇷🇮🇸🇮🇹🇯🇪🇯🇲🇯🇴🇯🇵🇰🇪🇰🇬🇰🇭🇰🇮🇰🇲🇰🇳🇰🇵🇰🇷🇰🇼🇰🇾🇰🇿🇱🇦🇱🇧🇱🇨🇱🇮🇱🇰🇱🇷🇱🇸🇱🇹🇱🇺🇱🇻🇱🇾🇲🇦🇲🇨🇲🇩🇲🇪🇲🇫🇲🇬🇲🇭🇲🇰" +
|
|
31
|
+
"🇲🇱🇲🇲🇲🇳🇲🇴🇲🇵🇲🇶🇲🇷🇲🇸🇲🇹🇲🇺🇲🇻🇲🇼🇲🇽🇲🇾🇲🇿🇳🇦🇳🇨🇳🇪🇳🇫🇳🇬🇳🇮🇳🇱🇳🇴🇳🇵🇳🇷🇳🇺🇳🇿🇴🇲🇵🇦🇵🇪🇵🇫🇵🇬🇵🇭🇵🇰🇵🇱🇵🇲🇵🇳🇵🇷🇵🇸🇵🇹🇵🇼🇵🇾🇶🇦🇷🇪🇷🇴🇷🇸🇷🇺🇷🇼🇸🇦🇸🇧🇸🇨🇸🇩🇸🇪🇸🇬🇸🇭🇸🇮🇸🇯🇸🇰🇸🇱🇸🇲🇸🇳🇸🇴🇸🇷🇸🇸🇸🇹🇸🇻🇸🇽🇸🇾🇸🇿" +
|
|
32
|
+
"🇹🇦🇹🇨🇹🇩🇹🇫🇹🇬🇹🇭🇹🇯🇹🇰🇹🇱🇹🇲🇹🇳🇹🇴🇹🇷🇹🇹🇹🇻🇹🇼🇹🇿🇺🇦🇺🇬🇺🇲🇺🇳🇺🇸🇺🇾🇺🇿🇻🇦🇻🇨🇻🇪🇻🇬🇻🇮🇻🇳🇻🇺🇼🇫🇼🇸🇽🇰🇾🇪🇾🇹🇿🇦🇿🇲🇿🇼🏴🏴🏴☺︎☹︎☠︎❣︎❤︎☘︎⛸︎♠︎♥︎♦︎♣︎♟︎⛷︎⛰︎⛩︎♨︎⛴︎✈︎☀︎⏱︎⏲︎☁︎⛈︎☂︎⛱︎❄︎☃︎☄︎⛑︎☎︎⌨︎✏︎✒︎✉︎✂︎⛏︎⚒︎⚔︎⚙︎⚖︎⛓︎⚗︎⚰︎⚱︎⚠︎☢︎☣︎⬆︎" +
|
|
33
|
+
"↗︎➡︎↘︎⬇︎↙︎⬅︎↖︎↕︎↔︎↩︎↪︎⤴︎⤵︎⚛︎✡︎☸︎☯︎✝︎☦︎☪︎☮︎▶︎⏭︎⏯︎◀︎⏮︎⏸︎⏹︎⏺︎⏏︎♀︎♂︎⚧︎✖︎♾︎‼︎⁉︎⚕︎♻︎⚜︎☑︎✔︎〽︎✳︎✴︎❇︎©︎®︎™︎🅰︎🅱︎ℹ︎Ⓜ︎🅾︎🅿︎🈂︎🈷︎㊗︎㊙︎◼︎◻︎▪︎▫︎";
|
|
34
|
+
module.exports = emoji;
|
package/errors.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const os = require('os');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
|
|
5
|
+
const logPath = path.join(os.homedir(), '.npak-error.log');
|
|
6
|
+
|
|
7
|
+
function handleHumanError(err, actionName) {
|
|
8
|
+
let reason = err.message;
|
|
9
|
+
|
|
10
|
+
if (err.code === 'ENOENT') {
|
|
11
|
+
reason = `Не найден указанный файл или папка: "${err.path}"`;
|
|
12
|
+
} else if (err.code === 'EACCES' || err.code === 'EPERM') {
|
|
13
|
+
reason = `Нет прав доступа для чтения или записи: "${err.path}"`;
|
|
14
|
+
} else if (err.code === 'EISDIR') {
|
|
15
|
+
reason = `Ожидался файл, но вместо него указана папка: "${err.path}"`;
|
|
16
|
+
} else if (err.code === 'ENOTDIR') {
|
|
17
|
+
reason = `Ожидалась папка, но указан обычный файл: "${err.path}"`;
|
|
18
|
+
} else if (err.isCustom) {
|
|
19
|
+
reason = err.message;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
console.error(`\x1b[31m[ОШИБКА при операции "${actionName}"] Причина: ${reason}\x1b[0m`);
|
|
23
|
+
|
|
24
|
+
const time = new Date().toLocaleString('ru-RU');
|
|
25
|
+
const logLine = `[${time}] Операция: ${actionName} | Причина: ${reason}\n`;
|
|
26
|
+
fs.appendFileSync(logPath, logLine, 'utf8');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
module.exports = { handleHumanError };
|
package/index.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const { execFileSync } = require("child_process");
|
|
4
|
+
const { packTargets } = require(path.join(__dirname, "pack.js"));
|
|
5
|
+
const { unpackArchive } = require(path.join(__dirname, "unpack.js"));
|
|
6
|
+
const { handleHumanError } = require(path.join(__dirname, "errors.js"));
|
|
7
|
+
const rawArgs = process.argv.slice(2);
|
|
8
|
+
const args = [];
|
|
9
|
+
let format = "npak";
|
|
10
|
+
let formatExplicitlySet = false;
|
|
11
|
+
for (let i = 0; i < rawArgs.length; i++) {
|
|
12
|
+
if ((rawArgs[i] === "--format" || rawArgs[i] === "-f") && rawArgs[i + 1]) {
|
|
13
|
+
format = rawArgs[i + 1].toLowerCase().replace(".", "");
|
|
14
|
+
formatExplicitlySet = true;
|
|
15
|
+
i++;
|
|
16
|
+
} else {
|
|
17
|
+
args.push(rawArgs[i]);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const command = args;
|
|
21
|
+
function delegateToZip(action, sources, target) {
|
|
22
|
+
try {
|
|
23
|
+
const isWin = process.platform === "win32";
|
|
24
|
+
|
|
25
|
+
if (action === "pack") {
|
|
26
|
+
const outZip = target.endsWith(".zip") ? target : `${target}.zip`;
|
|
27
|
+
const sourceList = Array.isArray(sources) ? sources : [sources];
|
|
28
|
+
|
|
29
|
+
if (isWin) {
|
|
30
|
+
execFileSync("tar", ["-a", "-cf", outZip, ...sourceList], {
|
|
31
|
+
stdio: "inherit",
|
|
32
|
+
});
|
|
33
|
+
} else {
|
|
34
|
+
execFileSync(
|
|
35
|
+
"python3",
|
|
36
|
+
["-m", "zipfile", "-c", outZip, ...sourceList],
|
|
37
|
+
{ stdio: "inherit" },
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
console.log(
|
|
41
|
+
`\x1b[32m[ZIP] Готово! Задача передана системному архиватору -> ${outZip}\x1b[0m`,
|
|
42
|
+
);
|
|
43
|
+
} else if (action === "unpack") {
|
|
44
|
+
if (!fs.existsSync(sources)) {
|
|
45
|
+
const e = new Error(`ZIP-архив "${sources}" не найден.`);
|
|
46
|
+
e.isCustom = true;
|
|
47
|
+
throw e;
|
|
48
|
+
}
|
|
49
|
+
fs.mkdirSync(target, { recursive: true });
|
|
50
|
+
|
|
51
|
+
if (isWin) {
|
|
52
|
+
execFileSync("tar", ["-xf", sources, "-C", target], {
|
|
53
|
+
stdio: "inherit",
|
|
54
|
+
});
|
|
55
|
+
} else {
|
|
56
|
+
execFileSync("python3", ["-m", "zipfile", "-e", sources, target], {
|
|
57
|
+
stdio: "inherit",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
console.log(
|
|
61
|
+
`\x1b[32m[ZIP] Успешно распаковано в папку: ${target}\x1b[0m`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
} catch (err) {
|
|
65
|
+
handleHumanError(
|
|
66
|
+
err,
|
|
67
|
+
`ZIP-${action === "pack" ? "упаковка" : "распаковка"}`,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if ((command === "pack" || command === "p") && args && args) {
|
|
72
|
+
const source = args;
|
|
73
|
+
const target = args;
|
|
74
|
+
|
|
75
|
+
if (format === "zip" || (!formatExplicitlySet && target.endsWith(".zip"))) {
|
|
76
|
+
delegateToZip("pack", source, target);
|
|
77
|
+
} else {
|
|
78
|
+
packTargets(source, target);
|
|
79
|
+
}
|
|
80
|
+
} else if ((command === "unpack" || command === "u") && args && args) {
|
|
81
|
+
const archive = args;
|
|
82
|
+
const targetDir = args;
|
|
83
|
+
|
|
84
|
+
if (format === "zip" || (!formatExplicitlySet && archive.endsWith(".zip"))) {
|
|
85
|
+
delegateToZip("unpack", archive, targetDir);
|
|
86
|
+
} else {
|
|
87
|
+
unpackArchive(archive, targetDir);
|
|
88
|
+
}
|
|
89
|
+
} else if (command === "pack-auto" && args.length > 1) {
|
|
90
|
+
const targets = args.slice(1).map((p) => path.resolve(p));
|
|
91
|
+
const firstItem = targets;
|
|
92
|
+
const parentDir = path.dirname(firstItem);
|
|
93
|
+
const baseName =
|
|
94
|
+
targets.length === 1 ? path.parse(firstItem).name : "archive";
|
|
95
|
+
|
|
96
|
+
if (format === "zip") {
|
|
97
|
+
const outZip = path.join(parentDir, `${baseName}.zip`);
|
|
98
|
+
delegateToZip("pack", targets, outZip);
|
|
99
|
+
} else {
|
|
100
|
+
const outArchive = path.join(parentDir, `${baseName}.npak`);
|
|
101
|
+
packTargets(targets, outArchive);
|
|
102
|
+
}
|
|
103
|
+
} else if (command && (command.endsWith(".npak") || command.endsWith(".zip"))) {
|
|
104
|
+
const archivePath = path.resolve(command);
|
|
105
|
+
const ext = path.extname(archivePath);
|
|
106
|
+
const folderName = path.basename(archivePath, ext);
|
|
107
|
+
const outputDir = path.join(path.dirname(archivePath), folderName);
|
|
108
|
+
|
|
109
|
+
if (ext === ".zip" || format === "zip") {
|
|
110
|
+
delegateToZip("unpack", archivePath, outputDir);
|
|
111
|
+
} else {
|
|
112
|
+
unpackArchive(archivePath, outputDir);
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
console.log("Использование:");
|
|
116
|
+
console.log(" Упаковать: npak pack ./my_project ./archive.npak");
|
|
117
|
+
console.log(" Распаковать: npak unpack ./archive.npak ./restored");
|
|
118
|
+
}
|
package/npak-icon.png
ADDED
|
Binary file
|
package/pack.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const crypto = require("crypto");
|
|
4
|
+
const rawPalette = require("chalk-palette");
|
|
5
|
+
const chalk_palette = rawPalette.default || rawPalette;
|
|
6
|
+
const { NIBBLE_MAP, CHAR_TO_NUM } = require(path.join(__dirname, "codes.js"));
|
|
7
|
+
const { handleHumanError } = require(path.join(__dirname, "errors.js"));
|
|
8
|
+
|
|
9
|
+
class DiskBitWriter {
|
|
10
|
+
constructor(fd) {
|
|
11
|
+
this.fd = fd;
|
|
12
|
+
this.hash = crypto.createHash("sha256");
|
|
13
|
+
this.chunkBuf = Buffer.alloc(65536);
|
|
14
|
+
this.offset = 0;
|
|
15
|
+
this.highNibble = null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
writeNibble(nibble) {
|
|
19
|
+
if (this.highNibble === null) {
|
|
20
|
+
this.highNibble = nibble;
|
|
21
|
+
} else {
|
|
22
|
+
const byte = (this.highNibble << 4) | nibble;
|
|
23
|
+
this.highNibble = null;
|
|
24
|
+
this.chunkBuf[this.offset++] = byte;
|
|
25
|
+
if (this.offset === this.chunkBuf.length) {
|
|
26
|
+
this.flush();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
alignByte() {
|
|
32
|
+
if (this.highNibble !== null) {
|
|
33
|
+
this.writeNibble(0);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
writeRawBuffer(buf) {
|
|
38
|
+
this.flush();
|
|
39
|
+
this.hash.update(buf);
|
|
40
|
+
fs.writeSync(this.fd, buf);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
writeString(str) {
|
|
44
|
+
for (const char of str) {
|
|
45
|
+
const code = CHAR_TO_NUM[char];
|
|
46
|
+
if (code !== undefined) {
|
|
47
|
+
const numStr = String(code);
|
|
48
|
+
for (let i = 0; i < numStr.length; i++) {
|
|
49
|
+
this.writeNibble(NIBBLE_MAP[numStr[i]]);
|
|
50
|
+
}
|
|
51
|
+
this.writeNibble(NIBBLE_MAP["SEP"]);
|
|
52
|
+
} else {
|
|
53
|
+
const fallback = String(CHAR_TO_NUM["?"]);
|
|
54
|
+
for (let i = 0; i < fallback.length; i++) {
|
|
55
|
+
this.writeNibble(NIBBLE_MAP[fallback[i]]);
|
|
56
|
+
}
|
|
57
|
+
this.writeNibble(NIBBLE_MAP["SEP"]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
writeBinaryFile(fileBuf) {
|
|
63
|
+
this.writeNibble(NIBBLE_MAP["CMD_BIN"]);
|
|
64
|
+
this.alignByte();
|
|
65
|
+
|
|
66
|
+
const sizeHeader = Buffer.alloc(4);
|
|
67
|
+
sizeHeader.writeUInt32BE(fileBuf.length, 0);
|
|
68
|
+
this.writeRawBuffer(sizeHeader);
|
|
69
|
+
this.writeRawBuffer(fileBuf);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
flush() {
|
|
73
|
+
if (this.offset > 0) {
|
|
74
|
+
const slice = this.chunkBuf.subarray(0, this.offset);
|
|
75
|
+
this.hash.update(slice);
|
|
76
|
+
fs.writeSync(this.fd, slice);
|
|
77
|
+
this.offset = 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
finish() {
|
|
82
|
+
let padding = 0;
|
|
83
|
+
if (this.highNibble !== null) {
|
|
84
|
+
padding = 4;
|
|
85
|
+
this.writeNibble(0);
|
|
86
|
+
}
|
|
87
|
+
this.flush();
|
|
88
|
+
return {
|
|
89
|
+
padding,
|
|
90
|
+
checksum: this.hash.digest().subarray(0, 4),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function canCompressAsText(buffer) {
|
|
96
|
+
if (buffer.length === 0) return true;
|
|
97
|
+
for (let i = 0; i < Math.min(buffer.length, 4096); i++) {
|
|
98
|
+
if (buffer[i] === 0) return false;
|
|
99
|
+
}
|
|
100
|
+
const str = buffer.toString("utf8");
|
|
101
|
+
if (str.includes("\uFFFD")) return false;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function countTotalItems(targetPath) {
|
|
106
|
+
const stat = fs.statSync(targetPath);
|
|
107
|
+
if (stat.isFile()) return 1;
|
|
108
|
+
|
|
109
|
+
let count = 0;
|
|
110
|
+
const items = fs.readdirSync(targetPath, { withFileTypes: true });
|
|
111
|
+
for (const item of items) {
|
|
112
|
+
count++;
|
|
113
|
+
if (item.isDirectory()) {
|
|
114
|
+
count += countTotalItems(path.join(targetPath, item.name));
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return count;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function packTargets(sources, outputArchivePath) {
|
|
121
|
+
let fd = null;
|
|
122
|
+
try {
|
|
123
|
+
const sourceList = Array.isArray(sources) ? sources : [sources];
|
|
124
|
+
|
|
125
|
+
if (!outputArchivePath.endsWith(".npak")) {
|
|
126
|
+
outputArchivePath += ".npak";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let totalSteps = 0;
|
|
130
|
+
for (const src of sourceList) {
|
|
131
|
+
if (!fs.existsSync(src)) {
|
|
132
|
+
const e = new Error(`Путь "${src}" не существует.`);
|
|
133
|
+
e.isCustom = true;
|
|
134
|
+
throw e;
|
|
135
|
+
}
|
|
136
|
+
totalSteps += countTotalItems(src);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (totalSteps === 0) {
|
|
140
|
+
const e = new Error(`Выбранная папка пустая — нечего упаковывать.`);
|
|
141
|
+
e.isCustom = true;
|
|
142
|
+
throw e;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
fd = fs.openSync(outputArchivePath, "w");
|
|
146
|
+
fs.writeSync(fd, Buffer.alloc(9));
|
|
147
|
+
|
|
148
|
+
const writer = new DiskBitWriter(fd);
|
|
149
|
+
const bar = chalk_palette.animation.progressBar(totalSteps);
|
|
150
|
+
let currentStep = 0;
|
|
151
|
+
|
|
152
|
+
function writeSingleFile(fullPath, relPath) {
|
|
153
|
+
writer.writeNibble(NIBBLE_MAP["CMD_FILE"]);
|
|
154
|
+
writer.writeString(relPath);
|
|
155
|
+
|
|
156
|
+
const fileBuf = fs.readFileSync(fullPath);
|
|
157
|
+
|
|
158
|
+
if (canCompressAsText(fileBuf)) {
|
|
159
|
+
writer.writeNibble(NIBBLE_MAP["CMD_DATA"]);
|
|
160
|
+
writer.writeString(fileBuf.toString("utf8"));
|
|
161
|
+
writer.writeNibble(NIBBLE_MAP["CMD_END"]);
|
|
162
|
+
} else {
|
|
163
|
+
writer.writeBinaryFile(fileBuf);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
currentStep++;
|
|
167
|
+
bar.update(currentStep);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function walkDir(baseRoot, currentPath) {
|
|
171
|
+
const items = fs.readdirSync(currentPath, { withFileTypes: true });
|
|
172
|
+
|
|
173
|
+
for (const item of items) {
|
|
174
|
+
const fullPath = path.join(currentPath, item.name);
|
|
175
|
+
const relPath = path.relative(baseRoot, fullPath).replace(/\\/g, "/");
|
|
176
|
+
|
|
177
|
+
if (item.isDirectory()) {
|
|
178
|
+
writer.writeNibble(NIBBLE_MAP["CMD_DIR"]);
|
|
179
|
+
writer.writeString(relPath);
|
|
180
|
+
writer.writeNibble(NIBBLE_MAP["CMD_END"]);
|
|
181
|
+
|
|
182
|
+
currentStep++;
|
|
183
|
+
bar.update(currentStep);
|
|
184
|
+
|
|
185
|
+
walkDir(baseRoot, fullPath);
|
|
186
|
+
} else if (item.isFile()) {
|
|
187
|
+
writeSingleFile(fullPath, relPath);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
for (const src of sourceList) {
|
|
193
|
+
const stat = fs.statSync(src);
|
|
194
|
+
if (stat.isFile()) {
|
|
195
|
+
writeSingleFile(src, path.basename(src));
|
|
196
|
+
} else if (stat.isDirectory()) {
|
|
197
|
+
if (sourceList.length > 1) {
|
|
198
|
+
const folderName = path.basename(src);
|
|
199
|
+
writer.writeNibble(NIBBLE_MAP["CMD_DIR"]);
|
|
200
|
+
writer.writeString(folderName);
|
|
201
|
+
writer.writeNibble(NIBBLE_MAP["CMD_END"]);
|
|
202
|
+
walkDir(path.dirname(src), src);
|
|
203
|
+
} else {
|
|
204
|
+
walkDir(src, src);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const { padding, checksum } = writer.finish();
|
|
210
|
+
const header = Buffer.concat([
|
|
211
|
+
Buffer.from("NPAK"),
|
|
212
|
+
checksum,
|
|
213
|
+
Buffer.from([padding]),
|
|
214
|
+
]);
|
|
215
|
+
fs.writeSync(fd, header, 0, 9, 0);
|
|
216
|
+
|
|
217
|
+
const finalSize = fs.fstatSync(fd).size;
|
|
218
|
+
fs.closeSync(fd);
|
|
219
|
+
fd = null;
|
|
220
|
+
|
|
221
|
+
const sizeMB = (finalSize / (1024 * 1024)).toFixed(2);
|
|
222
|
+
console.log(
|
|
223
|
+
`\n\x1b[32mГотово! Упаковано в ${outputArchivePath} (${sizeMB} МБ / ${finalSize} байт)\x1b[0m`,
|
|
224
|
+
);
|
|
225
|
+
} catch (err) {
|
|
226
|
+
if (fd !== null) fs.closeSync(fd);
|
|
227
|
+
handleHumanError(err, "Упаковка в .npak");
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
module.exports = { packFolder: packTargets, packTargets };
|
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "npak-archiver",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Кроссплатформенный бинарный архиватор формата .npak",
|
|
5
|
+
"main": "api.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"npak": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"index.js",
|
|
11
|
+
"api.js",
|
|
12
|
+
"pack.js",
|
|
13
|
+
"unpack.js",
|
|
14
|
+
"codes.js",
|
|
15
|
+
"emoji_codes.js",
|
|
16
|
+
"cjk.js",
|
|
17
|
+
"errors.js",
|
|
18
|
+
"npak-icon.png",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"chalk-palette": "*"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"archiver",
|
|
26
|
+
"npak",
|
|
27
|
+
"cli",
|
|
28
|
+
"compression"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT"
|
|
31
|
+
}
|
package/unpack.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
const fs = require("fs");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const crypto = require("crypto");
|
|
4
|
+
const rawPalette = require("chalk-palette");
|
|
5
|
+
const chalk_palette = rawPalette.default || rawPalette;
|
|
6
|
+
const { REV_NIBBLE_MAP, NUM_TO_CHAR } = require(
|
|
7
|
+
path.join(__dirname, "codes.js"),
|
|
8
|
+
);
|
|
9
|
+
const { handleHumanError } = require(path.join(__dirname, "errors.js"));
|
|
10
|
+
|
|
11
|
+
function unpackArchive(archivePath, targetFolder) {
|
|
12
|
+
try {
|
|
13
|
+
if (!fs.existsSync(archivePath)) {
|
|
14
|
+
const e = new Error(`Файл архива "${archivePath}" не найден на диске.`);
|
|
15
|
+
e.isCustom = true;
|
|
16
|
+
throw e;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const buf = fs.readFileSync(archivePath);
|
|
20
|
+
|
|
21
|
+
if (buf.length < 10) {
|
|
22
|
+
const e = new Error(`Файл "${archivePath}" слишком мал или поврежден.`);
|
|
23
|
+
e.isCustom = true;
|
|
24
|
+
throw e;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (buf.subarray(0, 4).toString() !== "NPAK") {
|
|
28
|
+
const e = new Error(
|
|
29
|
+
`Файл "${archivePath}" не является архивом формата .npak.`,
|
|
30
|
+
);
|
|
31
|
+
e.isCustom = true;
|
|
32
|
+
throw e;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const savedChecksum = buf.subarray(4, 8);
|
|
36
|
+
const padding = buf[8];
|
|
37
|
+
const dataBytes = buf.subarray(9);
|
|
38
|
+
|
|
39
|
+
const actualChecksum = crypto
|
|
40
|
+
.createHash("sha256")
|
|
41
|
+
.update(dataBytes)
|
|
42
|
+
.digest()
|
|
43
|
+
.subarray(0, 4);
|
|
44
|
+
if (!savedChecksum.equals(actualChecksum)) {
|
|
45
|
+
const e = new Error(
|
|
46
|
+
`Целостность архива "${archivePath}" нарушена! Файл изменен или поврежден.`,
|
|
47
|
+
);
|
|
48
|
+
e.isCustom = true;
|
|
49
|
+
throw e;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let mode = null;
|
|
53
|
+
let currentPath = "";
|
|
54
|
+
let textChunks = [];
|
|
55
|
+
let digitBuffer = "";
|
|
56
|
+
|
|
57
|
+
fs.mkdirSync(targetFolder, { recursive: true });
|
|
58
|
+
|
|
59
|
+
let byteIdx = 0;
|
|
60
|
+
let isLowNibble = false;
|
|
61
|
+
const totalBytes = dataBytes.length;
|
|
62
|
+
|
|
63
|
+
const bar = chalk_palette.animation.progressBar(100);
|
|
64
|
+
let lastPercent = 0;
|
|
65
|
+
|
|
66
|
+
while (byteIdx < totalBytes) {
|
|
67
|
+
const currentPercent = Math.floor((byteIdx / totalBytes) * 100);
|
|
68
|
+
if (currentPercent > lastPercent) {
|
|
69
|
+
lastPercent = currentPercent;
|
|
70
|
+
bar.update(lastPercent);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (byteIdx === totalBytes - 1 && isLowNibble && padding === 4) {
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const currentByte = dataBytes[byteIdx];
|
|
78
|
+
const nibble = isLowNibble ? currentByte & 0x0f : currentByte >> 4;
|
|
79
|
+
|
|
80
|
+
if (isLowNibble) {
|
|
81
|
+
byteIdx++;
|
|
82
|
+
isLowNibble = false;
|
|
83
|
+
} else {
|
|
84
|
+
isLowNibble = true;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const token = REV_NIBBLE_MAP[nibble];
|
|
88
|
+
|
|
89
|
+
if (token === "CMD_DIR") {
|
|
90
|
+
mode = "DIR";
|
|
91
|
+
currentPath = "";
|
|
92
|
+
} else if (token === "CMD_FILE") {
|
|
93
|
+
mode = "FILE_PATH";
|
|
94
|
+
currentPath = "";
|
|
95
|
+
} else if (token === "CMD_DATA") {
|
|
96
|
+
mode = "FILE_DATA";
|
|
97
|
+
textChunks = [];
|
|
98
|
+
} else if (token === "CMD_BIN") {
|
|
99
|
+
if (isLowNibble) {
|
|
100
|
+
byteIdx++;
|
|
101
|
+
isLowNibble = false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const binSize = dataBytes.readUInt32BE(byteIdx);
|
|
105
|
+
byteIdx += 4;
|
|
106
|
+
|
|
107
|
+
const binContent = dataBytes.subarray(byteIdx, byteIdx + binSize);
|
|
108
|
+
byteIdx += binSize;
|
|
109
|
+
|
|
110
|
+
const fullOutPath = path.join(targetFolder, currentPath);
|
|
111
|
+
fs.mkdirSync(path.dirname(fullOutPath), { recursive: true });
|
|
112
|
+
fs.writeFileSync(fullOutPath, binContent);
|
|
113
|
+
mode = null;
|
|
114
|
+
} else if (token === "CMD_END") {
|
|
115
|
+
if (mode === "DIR") {
|
|
116
|
+
fs.mkdirSync(path.join(targetFolder, currentPath), {
|
|
117
|
+
recursive: true,
|
|
118
|
+
});
|
|
119
|
+
} else if (mode === "FILE_DATA") {
|
|
120
|
+
const fullOutPath = path.join(targetFolder, currentPath);
|
|
121
|
+
fs.mkdirSync(path.dirname(fullOutPath), { recursive: true });
|
|
122
|
+
fs.writeFileSync(fullOutPath, textChunks.join(""), "utf8");
|
|
123
|
+
}
|
|
124
|
+
mode = null;
|
|
125
|
+
} else if (token === "SEP") {
|
|
126
|
+
const num = Number(digitBuffer);
|
|
127
|
+
const char = NUM_TO_CHAR[num];
|
|
128
|
+
if (mode === "DIR" || mode === "FILE_PATH") {
|
|
129
|
+
currentPath += char;
|
|
130
|
+
} else if (mode === "FILE_DATA") {
|
|
131
|
+
textChunks.push(char);
|
|
132
|
+
}
|
|
133
|
+
digitBuffer = "";
|
|
134
|
+
} else {
|
|
135
|
+
digitBuffer += token;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
bar.update(100);
|
|
140
|
+
console.log(
|
|
141
|
+
`\n\x1b[32mУспешно распаковано в папку: ${targetFolder}\x1b[0m`,
|
|
142
|
+
);
|
|
143
|
+
} catch (err) {
|
|
144
|
+
handleHumanError(err, "Распаковка архива");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
module.exports = { unpackArchive };
|