Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Zip Archiv erstellen

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Zip Archiv erstellen

    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      WolfgangFB last edited by

      Wie kann man in Javascript ein Zip Archiv (oder tar.gz) erstellen?

      function AddToZip(ZipFileName, FileToAdd){
          // Zip Archiv ZipFileName erzeugen wenn es nicht existiert, sonst FileToAdd in das Archiv packen
          ???
      }
      
      F 1 Reply Last reply Reply Quote 0
      • F
        fastfoot @WolfgangFB last edited by

        @wolfgangfb siehe hier

        // @ts-ignore
        const JSZip = require('jszip');
        // @ts-ignore
        const fse = require('fs-extra');
        const Path = require('path');
        const inputFolder = '../../scriptInput';
        const outputFolder = '../../scriptOutput';
        
        const imgData = fse.readFileSync(Path.resolve(inputFolder, 'test.png'));
        var zip = new JSZip();
        zip.file("Hello.txt", "Hello World\n");
        var img = zip.folder("images");
        img.file("test.png", imgData, { base64: true });
        zip.generateAsync({ type: "nodebuffer" })
           .then(function (content) {
               fse.writeFileSync(Path.resolve(outputFolder, "example.zip"), content);
           });
        

        1 Reply Last reply Reply Quote 0
        • W
          WolfgangFB last edited by

          @fastfoot
          Danke erstmal.
          Verstehe ich das richtig, dass ich die Datei erst mal laden muss? Damit würden aber die Attribute und Zeitsstempel der Datei, die ins Archiv gepackt wird, verloren gehen. Und bei sehr großen Dateien stelle ich mir das schwierig vor.
          Könnte man nicht auch einen Systemaufruf abschicken und das BS das ganze erledigen lassen?

          F 1 Reply Last reply Reply Quote 0
          • F
            fastfoot @WolfgangFB last edited by

            @wolfgangfb sagte in Zip Archiv erstellen:

            Könnte man nicht auch...

            klar, Kommando für dein OS finden und mit exec() aufrufen. für Linux würde ich zip nehmen, muss aber evtl. zuerst installiert werden

            W 1 Reply Last reply Reply Quote 0
            • W
              WolfgangFB @fastfoot last edited by

              @fastfoot sagte in Zip Archiv erstellen:

              @wolfgangfb sagte in Zip Archiv erstellen:

              Könnte man nicht auch...

              klar, Kommando für dein OS finden und mit exec() aufrufen. für Linux würde ich zip nehmen, muss aber evtl. zuerst installiert werden

              Kannst Du mir ein Beispiel nennen wie ich z.B. ein "ls -la /opt/iobroker > /opt/iobroker/hallo.txt" mit exec() realisieren würde.
              Alles was ich bei google über Javascript und exec finde läuft auf

              JavaScript exec() method is used to search for a matching string in a certain string.
              

              hinaus und das kann es ja wohl nicht sein.

              F 1 Reply Last reply Reply Quote 0
              • F
                fastfoot @WolfgangFB last edited by

                @wolfgangfb also da gibt es 100te Beispiele auch hier im Forum 🙂

                exec('ls -la /opt/iobroker >/opt/iobroker/hallo.txt', (err, out, e) => {
                    if (err) console.error(err);
                    if (e) console.error(e);
                    require('fs').readFile('/opt/iobroker/hallo.txt', 'utf8', (err, data) => {
                        if (err)
                            console.error(err);
                        else
                            console.log(data);
                
                    })
                })
                
                1 Reply Last reply Reply Quote 0
                • First post
                  Last post

                Support us

                ioBroker
                Community Adapters
                Donate

                539
                Online

                31.9k
                Users

                80.1k
                Topics

                1.3m
                Posts

                2
                6
                216
                Loading More Posts
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes
                Reply
                • Reply as topic
                Log in to reply
                Community
                Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                The ioBroker Community 2014-2023
                logo