Skip to content

Commit bf155bc

Browse files
committed
app added in index.md
2 parents 706c471 + 75ed1d2 commit bf155bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+10975
-20
lines changed

File Zipper/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<h1>File Zipper</h1>
2+
3+
<p>An Awesome encoder and decoder using huffman encoding techniques.</p>
4+
5+
### Use of the Project:
6+
7+
<p>It can encode and decode the text files only and downloads automatically . </p>
8+
9+
<h3>Used Technologies</h3>
10+
<ul>
11+
<li>HTML5</li>
12+
<li>CSS3</li>
13+
<li>JavaScript</li>
14+
<li>php</li>
15+
16+
</ul>
17+
18+
#### Steps to Use:
19+
20+
---
21+
22+
- Download or clone the repository
23+
24+
```
25+
git clone https://github.com/soma2000-lang/Web-dev-mini-projects.git
26+
```
27+
28+
- Go to the directory
29+
- Run the index.html file by copying the file path and pasting it in your browser
30+
- Upload The txt file
31+
-And there you have your Encoded or decoded file
32+
33+
<h3> ScreenShot </h3>
34+
<img width="960" alt="File_Zipper" src="https://github.com/soma2000-lang/File-Zipper/blob/main/WhatsApp%20Image%202021-07-20%20at%2022.04.38%20(1).jpeg">
35+
36+
37+
<br>
38+
35.5 KB
Loading

File Zipper/heap.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Created by aarnavjindal on 30/03/20.
3+
*/
4+
5+
export { BinaryHeap }
6+
7+
class BinaryHeap {
8+
9+
constructor() {
10+
this.heap = [];
11+
}
12+
13+
insert(value) {
14+
this.heap.push(value);
15+
this.bubbleUp();
16+
}
17+
18+
size() {
19+
return this.heap.length;
20+
}
21+
22+
empty(){
23+
return ( this.size()===0 );
24+
}
25+
26+
//using iterative approach
27+
bubbleUp() {
28+
let index = this.size() - 1;
29+
30+
while (index > 0) {
31+
let element = this.heap[index],
32+
parentIndex = Math.floor((index - 1) / 2),
33+
parent = this.heap[parentIndex];
34+
35+
if (parent[0] >= element[0]) break;
36+
this.heap[index] = parent;
37+
this.heap[parentIndex] = element;
38+
index = parentIndex
39+
}
40+
}
41+
42+
extractMax() {
43+
const max = this.heap[0];
44+
const tmp = this.heap.pop();
45+
if(!this.empty()) {
46+
this.heap[0] = tmp;
47+
this.sinkDown(0);
48+
}
49+
return max;
50+
}
51+
52+
sinkDown(index) {
53+
54+
let left = 2 * index + 1,
55+
right = 2 * index + 2,
56+
largest = index;
57+
const length = this.size();
58+
59+
// console.log(this.heap[left], left, length, this.heap[right], right, length, this.heap[largest]);
60+
61+
if (left < length && this.heap[left][0] > this.heap[largest][0]) {
62+
largest = left
63+
}
64+
if (right < length && this.heap[right][0] > this.heap[largest][0]) {
65+
largest = right
66+
}
67+
// swap
68+
if (largest !== index) {
69+
let tmp = this.heap[largest];
70+
this.heap[largest] = this.heap[index];
71+
this.heap[index] = tmp;
72+
this.sinkDown(largest)
73+
}
74+
}
75+
}

File Zipper/huffman.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* Created by aarnavjindal on 25/04/20.
3+
*/
4+
5+
import { BinaryHeap } from './heap.js';
6+
7+
export { HuffmanCoder }
8+
9+
class HuffmanCoder{
10+
11+
stringify(node){
12+
if(typeof(node[1])==="string"){
13+
return '\''+node[1];
14+
}
15+
16+
return '0' + this.stringify(node[1][0]) + '1' + this.stringify(node[1][1]);
17+
}
18+
19+
display(node, modify, index=1){
20+
if(modify){
21+
node = ['',node];
22+
if(node[1].length===1)
23+
node[1] = node[1][0];
24+
}
25+
26+
if(typeof(node[1])==="string"){
27+
return String(index) + " = " + node[1];
28+
}
29+
30+
let left = this.display(node[1][0], modify, index*2);
31+
let right = this.display(node[1][1], modify, index*2+1);
32+
let res = String(index*2)+" <= "+index+" => "+String(index*2+1);
33+
return res + '\n' + left + '\n' + right;
34+
}
35+
36+
destringify(data){
37+
let node = [];
38+
if(data[this.ind]==='\''){
39+
this.ind++;
40+
node.push(data[this.ind]);
41+
this.ind++;
42+
return node;
43+
}
44+
45+
this.ind++;
46+
let left = this.destringify(data);
47+
node.push(left);
48+
this.ind++;
49+
let right = this.destringify(data);
50+
node.push(right);
51+
52+
return node;
53+
}
54+
55+
getMappings(node, path){
56+
if(typeof(node[1])==="string"){
57+
this.mappings[node[1]] = path;
58+
return;
59+
}
60+
61+
this.getMappings(node[1][0], path+"0");
62+
this.getMappings(node[1][1], path+"1");
63+
}
64+
65+
encode(data){
66+
67+
this.heap = new BinaryHeap();
68+
69+
const mp = new Map();
70+
for(let i=0;i<data.length;i++){
71+
if(data[i] in mp){
72+
mp[data[i]] = mp[data[i]] + 1;
73+
} else{
74+
mp[data[i]] = 1;
75+
}
76+
}
77+
78+
for(const key in mp){
79+
this.heap.insert([-mp[key], key]);
80+
}
81+
82+
while(this.heap.size() > 1){
83+
const node1 = this.heap.extractMax();
84+
const node2 = this.heap.extractMax();
85+
86+
const node = [node1[0]+node2[0],[node1,node2]];
87+
this.heap.insert(node);
88+
}
89+
const huffman_encoder = this.heap.extractMax();
90+
91+
this.mappings = {};
92+
this.getMappings(huffman_encoder, "");
93+
94+
let binary_string = "";
95+
for(let i=0;i<data.length;i++) {
96+
binary_string = binary_string + this.mappings[data[i]];
97+
}
98+
99+
let rem = (8 - binary_string.length%8)%8;
100+
let padding = "";
101+
for(let i=0;i<rem;i++)
102+
padding = padding + "0";
103+
binary_string = binary_string + padding;
104+
105+
let result = "";
106+
for(let i=0;i<binary_string.length;i+=8){
107+
let num = 0;
108+
for(let j=0;j<8;j++){
109+
num = num*2 + (binary_string[i+j]-"0");
110+
}
111+
result = result + String.fromCharCode(num);
112+
}
113+
114+
let final_res = this.stringify(huffman_encoder) + '\n' + rem + '\n' + result;
115+
let info = "Compression Ratio : " + data.length/final_res.length;
116+
info = "Compression complete and file sent for download" + '\n' + info;
117+
return [final_res, this.display(huffman_encoder, false), info];
118+
}
119+
120+
decode(data){
121+
data = data.split('\n');
122+
if(data.length===4){
123+
// Handling new line
124+
data[0] = data[0] + '\n' + data[1];
125+
data[1] = data[2];
126+
data[2] = data[3];
127+
data.pop();
128+
}
129+
130+
this.ind = 0;
131+
const huffman_decoder = this.destringify(data[0]);
132+
const text = data[2];
133+
134+
let binary_string = "";
135+
for(let i=0;i<text.length;i++){
136+
let num = text[i].charCodeAt(0);
137+
let bin = "";
138+
for(let j=0;j<8;j++){
139+
bin = num%2 + bin;
140+
num = Math.floor(num/2);
141+
}
142+
binary_string = binary_string + bin;
143+
}
144+
binary_string = binary_string.substring(0,binary_string.length-data[1]);
145+
146+
console.log(binary_string.length);
147+
148+
let res = "";
149+
let node = huffman_decoder;
150+
for(let i=0;i<binary_string.length;i++){
151+
if(binary_string[i]==='0'){
152+
node = node[0];
153+
} else{
154+
node = node[1];
155+
}
156+
157+
if(typeof(node[0])==="string"){
158+
res += node[0];
159+
node = huffman_decoder;
160+
}
161+
}
162+
let info = "Decompression complete and file sent for download";
163+
return [res, this.display(huffman_decoder, true), info];
164+
}
165+
}

File Zipper/index.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
9+
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
10+
11+
<link rel="stylesheet" href="style.css">
12+
<script src="heap.js"></script>
13+
<script src="huffman.js" type="module"></script>
14+
<script src="script.js" type="module"></script>
15+
</head>
16+
17+
<body>
18+
<nav class="navbar navbar-light " style="font-size: 25px; font-family: sans-serif; background-color: whitesmoke;">
19+
20+
21+
File Zipper using huffman Encoding and Decoding
22+
</nav>
23+
<div id="container">
24+
<div class="text_box" style="overflow-y: scroll">
25+
<span id="treearea" style="width: 100%; text-align: center; font-size: medium;">
26+
Tree Structure Will Be Displayed Here !!
27+
</span>
28+
</div>
29+
<div class="text_box" style="overflow-y: scroll">
30+
<span id="temptext" style="width: 100%; text-align: center; font-size: x-large;">
31+
Operation info will be shown here !!
32+
</span>
33+
</div>
34+
</div>
35+
36+
<div>
37+
<form method="post" enctype="multipart/form-data" style="display: inline-block;">
38+
<input type="file" id="uploadedFile"/>
39+
</form>
40+
<br>
41+
<button type="button" class="btn btn-success center_buttons" id="encode">&nbsp;&nbsp;Encode&nbsp;&nbsp;</button>
42+
<br>
43+
<button type="button" class="btn btn-danger center_buttons" id="decode">&nbsp;&nbsp;Decode&nbsp;&nbsp;</button>
44+
</div>
45+
</body>
46+
47+
</html>

File Zipper/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php include_once("index.html"); ?>

0 commit comments

Comments
 (0)