Advertisement
Thunder-Menu

PakFile.html

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