-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[code.haxe.org] Other - Zip files #41
Comments
Works fine on Windows, but on Linux created zip is broken and cannot be opened. |
Which target? Works fine for me at least on cpp, neko and php |
I am using hxp. Code sampleimport 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";
}
} |
Afaik hxp uses neko. It works for me with pure neko without hxp. That may be a hxp bug. |
On Haxe 4 it uses eval:
|
Checked it. Works on eval too. |
Does anyone know if we can create compressed zips too? |
@markknol Just before haxe.zip.Tools.compress(entry, 9); |
Unzip Functionstatic 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();
}
}
} |
Interp on win64 needs |
Zip files - Other - Haxe programming language cookbook
The haxe.zip package allows to zip and unzip files and directories using Haxe. This example shows how to use it.
https://code.haxe.org/category/other/haxe-zip.html
The text was updated successfully, but these errors were encountered: