Code Monkey home page Code Monkey logo

Comments (10)

cedx avatar cedx commented on June 19, 2024 1

@markknol Just before entries.push(entry); ...

haxe.zip.Tools.compress(entry, 9);

from haxe.org-comments.

CrazyFlasher avatar CrazyFlasher commented on June 19, 2024

Works fine on Windows, but on Linux created zip is broken and cannot be opened.

from haxe.org-comments.

RealyUniqueName avatar RealyUniqueName commented on June 19, 2024

Which target? Works fine for me at least on cpp, neko and php

from haxe.org-comments.

CrazyFlasher avatar CrazyFlasher commented on June 19, 2024

I am using hxp.
Here is a full source of script.
As I said, works fine on Windows, but on Linux gives broken zip.

Code sample
import haxe.io.Path;
import sys.io.FileOutput;
import utils.FileUtils;
import sys.io.File;
import haxe.zip.Reader;
import haxe.zip.Writer;
import haxe.zip.Entry;
import sys.FileSystem;
import hxp.HXML;
import hxp.Script;

/**
* Compresses PNGs in directory using pngquant.
* Usage: haxelib run hxp CompressPNGs.hx -Din=<path to input folder>
*
* -Din - path to input directory
* -Dq - quality (0-100) (optional)
* -Dzip - will compress pngs inside zip archives (optional)
**/
class CompressPNGs extends Script
{
    private var input:String;

    private var quality:String = "0-90";
    private var checkZip:Bool = false;

    public function new()
    {
        super();

        if (!defines.exists("in"))
        {
            trace("Path to input directory is not specified!");
            trace("Define it as flag -Din=path_to_dir...");
            Sys.exit(1);
        }
        if (!defines.exists("q"))
        {
            trace("Quality no specified. Using default: 0-90...");
        } else
        {
            quality = Std.string(defines.get("q"));
        }
        if (defines.exists("zip"))
        {
            checkZip = true;

            trace("Will check inside zip files also...");
        }

        input = Path.join([workingDirectory, defines.get("in")]);

        compressDir(input);
    }

    private function compressDir(path:String):Void
    {
        if (FileSystem.exists(path) && FileSystem.isDirectory(path))
        {
            for (filePath in FileSystem.readDirectory(path))
            {
                var p:String = Path.join([path, filePath]);

                if (FileSystem.isDirectory(p))
                {
                    compressDir(p);
                } else
                {
                    if (isPNG(filePath))
                    {
                        compressFile(p);
                    }else
                    if (isZIP(filePath) && checkZip)
                    {
                        trace("Unpacking zip file: " + p);

                        var reader:Reader = new Reader(File.read(p, true));
                        var list:List<Entry> = reader.read();

                        var dir:String = p + "_temp/";
                        FileUtils.deleteWithFiles(dir);
                        FileSystem.createDirectory(dir);

                        for (entry in list)
                        {
                            Reader.unzip(entry);

                            var filePath:String = Path.join([dir, entry.fileName]);
                            File.saveBytes(filePath, entry.data);

                            if (isPNG(entry.fileName))
                            {
                                compressFile(filePath);
                            }
                        }

                        // create the output file
                        var out:FileOutput = File.write(p, true);
                        // write the zip file
                        var writer:Writer = new Writer(out);
                        writer.write(getEntries(dir));

                        FileUtils.deleteWithFiles(dir);
                    }
                }
            }
        }
    }

    // recursive read a directory, add the file entries to the list
    private function getEntries(dir:String, entries:List<haxe.zip.Entry> = null, inDir:Null<String> = null)
    {
        if (entries == null) entries = new List<haxe.zip.Entry>();
        if (inDir == null) inDir = dir;
        for (file in sys.FileSystem.readDirectory(dir))
        {
            var path = haxe.io.Path.join([dir, file]);
            if (sys.FileSystem.isDirectory(path))
            {
                getEntries(path, entries, inDir);
            } else
            {
                var bytes:haxe.io.Bytes = haxe.io.Bytes.ofData(sys.io.File.getBytes(path).getData());
                var entry:haxe.zip.Entry = {
//                    fileName: file.toString(),
                    fileName: StringTools.replace(path, inDir, ""),
                    fileSize: bytes.length,
                    fileTime: Date.now(),
                    compressed: false,
                    dataSize: 0,
                    data: bytes,
                    crc32: haxe.crypto.Crc32.make(bytes)
                };
                entries.push(entry);
            }
        }
        return entries;
    }

    private function compressFile(path:String):Void
    {
        trace("Compress file: " + path);

        var result:Int = new HXML({
//            cmds: ["pngquant " + path + " --ext .png --force --speed 1 --posterize ARGB4444 --quality " + quality]
//            cmds: ["pngquant " + path + " --ext .png --force --speed 1 --nofs --quality " + quality]
            cmds: ["pngquant " + path + " --ext .png --force --speed 1 --posterize ARGB4444 --quality " + quality]
        }).build();
    }

    private function isPNG(fileName:String):Bool
    {
        return fileName.substr(fileName.length - 4) == ".png";
    }

    private function isZIP(fileName:String):Bool
    {
        return fileName.substr(fileName.length - 4) == ".zip";
    }
}

from haxe.org-comments.

RealyUniqueName avatar RealyUniqueName commented on June 19, 2024

Afaik hxp uses neko. It works for me with pure neko without hxp. That may be a hxp bug.

from haxe.org-comments.

Gama11 avatar Gama11 commented on June 19, 2024

On Haxe 4 it uses eval:

Using HXP works on Haxe 4 (using eval internally) or Haxe 3 (using Neko).

from haxe.org-comments.

RealyUniqueName avatar RealyUniqueName commented on June 19, 2024

Checked it. Works on eval too.

from haxe.org-comments.

markknol avatar markknol commented on June 19, 2024

Does anyone know if we can create compressed zips too?
Zipping a file with 7-Zip results in smaller size, because they use compression.

from haxe.org-comments.

condigital avatar condigital commented on June 19, 2024

Unzip Function

static function unzip(archivo:String, donde:String) {
	var zipfileBytes = File.getBytes(archivo);
	var bytesInput = new BytesInput(zipfileBytes);
	var reader = new Reader(bytesInput);
	var entries:List<Entry> = reader.read();
	for (_entry in entries) {
		var data = Reader.unzip(_entry);
		if (_entry.fileName.substring(_entry.fileName.lastIndexOf('/') + 1) == '' && _entry.data.toString() == '') {
			sys.FileSystem.createDirectory(donde + _entry.fileName);
		} else {
			var f = File.write(donde + _entry.fileName, true);
			f.write(data);
			f.close();
		}
	}
}

from haxe.org-comments.

matrefeytontias avatar matrefeytontias commented on June 19, 2024

Interp on win64 needs out.close() or the resulting zip file will be forever held by haxe.exe (and also show up as having no contents despite having the correct size). This is not a problem for running a script since haxe.exe usually exits instantly, but when running with, say, VS Code, the haxe instance stays up and this causes problems. Tested with an hxml file with --run in it ; I assume this means interp.

from haxe.org-comments.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.