Advertisement
Thunder-Menu

PakFiles.html

Aug 19th, 2023
1,372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 17.91 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="fr">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Compression et Compressor de Fichiers</title>
  7.     <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
  8. </head>
  9. <body>
  10.     <select id="operationSelect">
  11.         <option value="pakobest" selected>best compressor</option>
  12.         <option value="compress">Compresser</option>
  13.         <option value="encode">Encodage</option>
  14.         <option value="decode">Décodage</option>
  15.     </select>
  16.  
  17.     <div id="compressSection" style="display: none;">
  18.         <h2>Compression de Fichiers</h2>
  19.         <input type="file" id="fileInput" multiple>
  20.         <br>
  21.         <button id="compressButton" disabled>Compresser et Enregistrer</button>
  22.     </div>
  23.  
  24.     <div id="encodeSection" style="display: none;">
  25.         <h2>Générateur de Motif de Carrés - Encodage</h2>
  26.         <input type="file" id="encodeFileInput" style="display: none;">
  27.         <br>
  28.         <button id="loadButton">Sélectionner un fichier</button>
  29.         <br>
  30.         <button id="generateButton" disabled>Générer</button>
  31.         <br>
  32.         <button id="saveButton" disabled>Enregistrer le motif</button>
  33.         <br>
  34.         <textarea id="textBox" rows="10" cols="50"></textarea>
  35.         <br>
  36.         <div id="encodePictureBox"></div>
  37.     </div>
  38.  
  39.     <div id="decodeSection" style="display: none;">
  40.         <h2>Générateur de Motif de Carrés - Décodage</h2>
  41.         <input type="file" id="decodeFileInput" class="button">
  42.         <br>
  43.         <button id="decodeGenerateButton" class="button" disabled>Générer</button>
  44.         <br>
  45.         <button id="decodeSaveButton" class="button" style="display: none;">Enregistrer</button>
  46.         <br>
  47.         <textarea id="decodeTextBox" rows="10" cols="50" style="margin-top: 20px;" readonly></textarea>
  48.         <br>
  49.         <canvas id="decodePictureBox"></canvas>
  50.     </div>
  51.  
  52.     <div id="fileCompressorSection" style="display: none;">
  53.         <h2>File Compressor</h2>
  54.         <select id="fileComboBox" size="10"></select>
  55.         <br>
  56.         <button onclick="addFilesToComboBox()">Choisir des fichiers</button>
  57.         <br>
  58.         <input type="file" id="folderInput" multiple directory="" webkitdirectory="" style="display: none;" onchange="addFoldersToComboBox()">
  59.         <button onclick="document.getElementById('folderInput').click()">Choisir un dossier</button>
  60.         <br>
  61.         <button onclick="clearSelectedItems()">Effacer</button>
  62.         <br>
  63.         <button onclick="compressAndSave()">Compresser et Enregistrer</button>
  64.         <br>
  65.         <select id="compressionComboBox">
  66.             <option value="best">Best</option>
  67.             <option value="store">Store</option>
  68.             <option value="normal">Normal</option>
  69.             <option value="maximum">Maximum</option>
  70.         </select>
  71.         <br>
  72.         <select id="extensionComboBox">
  73.             <option value="zip">.zip</option>
  74.             <option value="rar">.rar</option>
  75.             <option value="iso">.iso</option>
  76.         </select>
  77.     </div>
  78.  
  79.     <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  80.  
  81.     <script>
  82.         let selectedFiles = [];
  83.  
  84.         function addFilesToComboBox() {
  85.             const fileInput = document.createElement("input");
  86.             fileInput.type = "file";
  87.             fileInput.multiple = true;
  88.             fileInput.onchange = function () {
  89.                 const fileComboBox = document.getElementById("fileComboBox");
  90.  
  91.                 for (let i = 0; i < fileInput.files.length; i++) {
  92.                    selectedFiles.push(fileInput.files[i]);
  93.  
  94.                    const option = document.createElement("option");
  95.                    option.text = fileInput.files[i].name;
  96.                    fileComboBox.add(option);
  97.                }
  98.            };
  99.            fileInput.click();
  100.        }
  101.  
  102.        function addFoldersToComboBox() {
  103.            const folderInput = document.getElementById("folderInput");
  104.            const fileComboBox = document.getElementById("fileComboBox");
  105.  
  106.            for (let i = 0; i < folderInput.files.length; i++) {
  107.                traverseDirectory(folderInput.files[i], "");
  108.            }
  109.        }
  110.  
  111.        async function traverseDirectory(item, parentPath) {
  112.            if (item.isDirectory) {
  113.                const reader = item.createReader();
  114.                const entries = await reader.readEntries();
  115.                for (const entry of entries) {
  116.                    const fullPath = `${parentPath}/${entry.name}`;
  117.                    traverseDirectory(entry, fullPath);
  118.                }
  119.            } else {
  120.                selectedFiles.push(item);
  121.  
  122.                const option = document.createElement("option");
  123.                option.text = parentPath + "/" + item.webkitRelativePath;
  124.                fileComboBox.add(option);
  125.            }
  126.        }
  127.  
  128.        function clearSelectedItems() {
  129.            const fileComboBox = document.getElementById("fileComboBox");
  130.            for (let i = fileComboBox.options.length - 1; i >= 0; i--) {
  131.                 if (fileComboBox.options[i].selected) {
  132.                     selectedFiles.splice(i, 1);
  133.                     fileComboBox.remove(i);
  134.                 }
  135.             }
  136.         }
  137.  
  138.         async function compressAndSave() {
  139.             const compressionLevel = document.getElementById("compressionComboBox").value;
  140.             const selectedExtension = document.getElementById("extensionComboBox").value;
  141.  
  142.             const compressedData = await compressData(selectedFiles, compressionLevel);
  143.  
  144.             const blob = new Blob([compressedData], { type: 'application/octet-stream' });
  145.             const link = document.createElement('a');
  146.             link.href = URL.createObjectURL(blob);
  147.  
  148.             // Demander le nom du fichier compressé sans l'extension
  149.             const compressedFileName = prompt("Entrer le nom du fichier compressé:", `fichiers_compresses`);
  150.             if (!compressedFileName) {
  151.                 return; // Annuler la compression si le nom de fichier est vide
  152.             }
  153.  
  154.             const fullFileName = `${compressedFileName}.${selectedExtension}`;
  155.             link.download = fullFileName;
  156.             link.click();
  157.         }
  158.  
  159.         async function compressData(files, compressionLevel) {
  160.             // Créer un nouveau FormData pour les fichiers et dossiers sélectionnés
  161.             const zip = new JSZip();
  162.  
  163.             for (const file of files) {
  164.                 const fileContent = await file.arrayBuffer();
  165.                 const filePath = file.webkitRelativePath || file.name;
  166.                 zip.file(filePath, fileContent);
  167.             }
  168.  
  169.             // Utiliser pako pour compresser les données
  170.             const options = { level: compressionLevel === 'store' ? 0 : (compressionLevel === 'maximum' ? 9 : 5) };
  171.             const dataToCompress = await zip.generateAsync({ type: "uint8array", compression: "DEFLATE", compressionOptions: options });
  172.  
  173.             return dataToCompress;
  174.         }
  175.  
  176.         $(document).ready(function() {
  177.             $("#operationSelect").change(function() {
  178.                 const selectedOperation = $(this).val();
  179.                 $("#compressSection, #encodeSection, #decodeSection, #fileCompressorSection").hide();
  180.  
  181.                 if (selectedOperation === "compress") {
  182.                     $("#compressSection").show();
  183.                 } else if (selectedOperation === "encode") {
  184.                     $("#encodeSection").show();
  185.                 } else if (selectedOperation === "decode") {
  186.                     $("#decodeSection").show();
  187.                 } else if (selectedOperation === "pakobest") {
  188.                     $("#fileCompressorSection").show();
  189.                 }
  190.             });
  191.  
  192.             // Gestionnaires d'événements pour la section de compression
  193.             const fileInput = document.getElementById("fileInput");
  194.             const compressButton = document.getElementById("compressButton");
  195.  
  196.             fileInput.addEventListener("change", function (e) {
  197.                 if (e.target.files.length > 0) {
  198.                     compressButton.disabled = false;
  199.                 }
  200.             });
  201.  
  202.             compressButton.addEventListener("click", function () {
  203.                 const filesToCompress = Array.from(fileInput.files);
  204.                 const zip = new JSZip();
  205.                
  206.                 const promises = filesToCompress.map(file => {
  207.                     return new Promise(resolve => {
  208.                         const reader = new FileReader();
  209.                         reader.onload = function () {
  210.                             zip.file(file.name, reader.result);
  211.                             resolve();
  212.                         };
  213.                         reader.readAsArrayBuffer(file);
  214.                     });
  215.                 });
  216.  
  217.                 Promise.all(promises).then(() => {
  218.                     const zipName = prompt("Entrez le nom du fichier ZIP :") || "compressed.zip";
  219.                     zip.generateAsync({ type: "blob" }).then(function (content) {
  220.                         const a = document.createElement("a");
  221.                         a.href = URL.createObjectURL(content);
  222.                         a.download = zipName;
  223.                         document.body.appendChild(a);
  224.                         a.click();
  225.                         document.body.removeChild(a);
  226.                         URL.revokeObjectURL(a.href);
  227.                     });
  228.                 });
  229.             });
  230.  
  231.                 loadButton.addEventListener("click", function () {
  232.         encodeFileInput.click();
  233.     });
  234.  
  235.     // Gestionnaire d'événement pour le chargement du fichier d'encodage
  236.     encodeFileInput.addEventListener("change", function (e) {
  237.         if (e.target.files.length > 0) {
  238.             const file = e.target.files[0];
  239.             const reader = new FileReader();
  240.             reader.onload = function (event) {
  241.                 // Convertir les données du fichier en binaire
  242.                 const arrayBuffer = event.target.result;
  243.                 const byteArray = new Uint8Array(arrayBuffer);
  244.                 let binaryText = "";
  245.                 byteArray.forEach(function (byte) {
  246.                     binaryText += byte.toString(2).padStart(8, "0");
  247.                 });
  248.  
  249.                 // Afficher le texte binaire dans la zone de texte
  250.                 const textBox = document.getElementById("textBox");
  251.                 textBox.value = binaryText;
  252.                 generateButton.disabled = false;
  253.             };
  254.             reader.readAsArrayBuffer(file);
  255.         }
  256.     });
  257.  
  258.     // Gestionnaire d'événement pour le bouton de génération de motif d'encodage
  259.     const generateButton = document.getElementById("generateButton");
  260.  
  261.     generateButton.addEventListener("click", function () {
  262.         const binaryText = textBox.value;
  263.         const imageSize = Math.ceil(Math.sqrt(binaryText.length));
  264.         const bitmap = new ImageData(imageSize, imageSize);
  265.  
  266.         for (let i = 0; i < binaryText.length; i++) {
  267.            const x = i % imageSize;
  268.            const y = Math.floor(i / imageSize);
  269.            const color = binaryText[i] === "1" ? [0, 0, 0, 255] : [255, 255, 255, 255];
  270.            const index = (x + y * imageSize) * 4;
  271.            bitmap.data[index] = color[0];
  272.            bitmap.data[index + 1] = color[1];
  273.            bitmap.data[index + 2] = color[2];
  274.            bitmap.data[index + 3] = color[3];
  275.        }
  276.  
  277.        // Créer un élément canvas pour afficher le motif généré
  278.        const canvas = document.createElement("canvas");
  279.        canvas.width = imageSize;
  280.        canvas.height = imageSize;
  281.        const context = canvas.getContext("2d");
  282.        context.putImageData(bitmap, 0, 0);
  283.  
  284.        // Afficher le motif généré dans la section d'encodage
  285.        const encodePictureBox = document.getElementById("encodePictureBox");
  286.        encodePictureBox.innerHTML = "";
  287.        encodePictureBox.appendChild(canvas);
  288.        saveButton.disabled = false;
  289.    });
  290.    // Gestionnaire d'événement pour le bouton d'enregistrement du motif d'encodage
  291.    const saveButton = document.getElementById("saveButton");
  292.  
  293.    saveButton.addEventListener("click", function () {
  294.        const canvas = encodePictureBox.querySelector("canvas");
  295.        const image = canvas.toDataURL("image/bmp").replace("image/bmp", "image/octet-stream");
  296.        const a = document.createElement("a");
  297.        const fileName = prompt("Entrez le nom du fichier BMP :", "motif.bmp") || "motif.bmp";
  298.        a.href = image;
  299.        a.download = fileName;
  300.        document.body.appendChild(a);
  301.        a.click();
  302.        document.body.removeChild(a);
  303.    });
  304.  
  305.    // Gestionnaire d'événement pour le chargement du fichier de décodage
  306.    const decodeFileInput = document.getElementById("decodeFileInput");
  307.    const decodeGenerateButton = document.getElementById("decodeGenerateButton");
  308.    const decodeSaveButton = document.getElementById("decodeSaveButton");
  309.    const decodePictureBox = document.getElementById("decodePictureBox");
  310.    const decodeTextBox = document.getElementById("decodeTextBox");
  311.    const decodeCanvas = decodePictureBox.getContext("2d");
  312.    let loadedBitmap = null;
  313.    let binaryText = "";
  314.    decodeFileInput.addEventListener("change", (event) => {
  315.         const file = event.target.files[0];
  316.         if (file) {
  317.             const reader = new FileReader();
  318.             reader.onload = (e) => {
  319.                 const image = new Image();
  320.                 image.src = e.target.result;
  321.                 image.onload = () => {
  322.                     decodePictureBox.width = image.width;
  323.                     decodePictureBox.height = image.height;
  324.                     decodeCanvas.drawImage(image, 0, 0);
  325.                     loadedBitmap = image;
  326.                     decodeGenerateButton.disabled = false;
  327.                 };
  328.             };
  329.             reader.readAsDataURL(file);
  330.         }
  331.     });
  332.  
  333.     decodeGenerateButton.addEventListener("click", () => {
  334.         if (!loadedBitmap) {
  335.             alert("Veuillez charger une image d'abord.");
  336.             return;
  337.         }
  338.  
  339.         const imageData = decodeCanvas.getImageData(0, 0, decodePictureBox.width, decodePictureBox.height);
  340.         binaryText = generateBinaryPattern(imageData);
  341.         decodeTextBox.value = binaryText;
  342.         decodeSaveButton.style.display = "block";
  343.     });
  344.  
  345.     decodeSaveButton.addEventListener("click", () => {
  346.         if (!binaryText.trim()) {
  347.             alert("Le contenu du TextBox est vide. Veuillez générer le motif d'abord.");
  348.             return;
  349.         }
  350.  
  351.         const binaryBytes = convertBinaryTextToBytes(binaryText);
  352.         const blob = new Blob([binaryBytes], { type: "application/zip" });
  353.         const a = document.createElement("a");
  354.         const fileName = prompt("Entrez le nom du fichier ZIP :", "binary_pattern.zip") || "binary_pattern.zip";
  355.         a.href = URL.createObjectURL(blob);
  356.         a.download = fileName;
  357.         document.body.appendChild(a);
  358.         a.click();
  359.         document.body.removeChild(a);
  360.         URL.revokeObjectURL(a.href);
  361.     });
  362.  
  363.             function generateBinaryPattern(imageData) {
  364.                 const threshold = 128;
  365.                 let binaryText = "";
  366.  
  367.                 for (let y = 0; y < imageData.height; y++) {
  368.                    for (let x = 0; x < imageData.width; x++) {
  369.                        const index = (y * imageData.width + x) * 4;
  370.                        const r = imageData.data[index];
  371.                        const g = imageData.data[index + 1];
  372.                        const b = imageData.data[index + 2];
  373.                        const luminance = (0.299 * r + 0.587 * g + 0.114 * b);
  374.                        binaryText += (luminance < threshold) ? "1" : "0";
  375.                    }
  376.                }
  377.  
  378.                return binaryText;
  379.            }
  380.  
  381.            function convertBinaryTextToBytes(binaryText) {
  382.                const byteCount = Math.ceil(binaryText.length / 8);
  383.                const bytes = new Uint8Array(byteCount);
  384.  
  385.                for (let i = 0; i < byteCount; i++) {
  386.                    const byteStart = i * 8;
  387.                    const byteEnd = byteStart + 8;
  388.                    const byte = binaryText.slice(byteStart, byteEnd);
  389.                    bytes[i] = parseInt(byte, 2);
  390.                }
  391.  
  392.                return bytes;
  393.            }
  394.    compressButton.addEventListener("click", function () {
  395.        // Récupérer les fichiers sélectionnés
  396.        const filesToCompress = Array.from(fileInput.files);
  397.        // Créer une instance JSZip
  398.        const zip = new JSZip();
  399.  
  400.        // Créer un tableau de promesses pour lire les fichiers et les ajouter à l'archive zip
  401.        const promises = filesToCompress.map(file => {
  402.             return new Promise(resolve => {
  403.                 const reader = new FileReader();
  404.                 reader.onload = function () {
  405.                     zip.file(file.name, reader.result);
  406.                     resolve();
  407.                 };
  408.                 reader.readAsArrayBuffer(file);
  409.             });
  410.         });
  411.  
  412.         // Attendre que toutes les promesses soient résolues, puis générer et télécharger l'archive zip
  413.         Promise.all(promises).then(() => {
  414.             const zipName = prompt("Entrez le nom du fichier ZIP :") || "compressed.zip";
  415.             zip.generateAsync({ type: "blob" }).then(function (content) {
  416.                 // Créer un lien de téléchargement
  417.                 const a = document.createElement("a");
  418.                 a.href = URL.createObjectURL(content);
  419.                 a.download = zipName;
  420.                 document.body.appendChild(a);
  421.                 a.click();
  422.                 document.body.removeChild(a);
  423.                 URL.revokeObjectURL(a.href);
  424.             });
  425.         });
  426.     });
  427. });
  428.     </script>
  429. </body>
  430. </html>
  431.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement