Comments (10)
@markknol Just before entries.push(entry);
...
haxe.zip.Tools.compress(entry, 9);
from haxe.org-comments.
Works fine on Windows, but on Linux created zip is broken and cannot be opened.
from haxe.org-comments.
Which target? Works fine for me at least on cpp, neko and php
from haxe.org-comments.
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.
Afaik hxp uses neko. It works for me with pure neko without hxp. That may be a hxp bug.
from haxe.org-comments.
On Haxe 4 it uses eval:
Using HXP works on Haxe 4 (using eval internally) or Haxe 3 (using Neko).
from haxe.org-comments.
Checked it. Works on eval too.
from haxe.org-comments.
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.
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.
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)
- [haxe.org/manual] Target Details HOT 1
- [haxe.org/manual] Generic HOT 2
- [haxe.org/manual] Null Safety HOT 2
- [haxe.org/manual] Blocks HOT 3
- [haxe.org/manual] Tail Recursion Elimination (TRE) HOT 1
- [haxe.org/manual] Module Sub-Types HOT 3
- [code.haxe.org] Macros - Working with compiler flags HOT 1
- [haxe.org/manual] Extractors HOT 2
- [haxe.org/manual] Array Access (Types - Abstract) HOT 2
- [haxe.org/manual] Overriding Methods HOT 1
- [haxe.org/manual] Variable capture HOT 2
- [haxe.org/manual] Tracing Types HOT 1
- [code.haxe.org] Beginner - Using numbers HOT 2
- [haxe.org/manual] Defines (Target Details - C++) HOT 7
- [haxe.org/manual] Import defaults / import.hx HOT 1
- [haxe.org/manual] Type Parameters (Type System) HOT 10
- [haxe.org/manual] Access Control HOT 1
- [haxe.org/manual] Inline (Class Fields - Access Modifier) HOT 1
- [haxe.org/manual] Encoding HOT 2
- [haxe.org/manual] Constraints HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from haxe.org-comments.