/*
解压缩模块
*/
var path = require("path");
var fs = require("fs");
var Transform = require("stream").Transform;
var ffi = require("ffi")  

var DLL = ffi.Library('resources/supzip.dll',{
 'DecompressZipDec': ['int', ['string', 'string']]
});

// console.log(DLL.DecompressZipDec('C:/Users/shanglt/Downloads/test.ccr', 'C:/Users/shanglt/Downloads/test'));

function mkdirp(dir, cb) {
    if (dir === ".") {
        return cb();
    }
    fs.stat(dir, function (err) {
        if (err == null) {
            return cb(); // already exists
        }
        var parent = path.dirname(dir);
        mkdirp(parent, function () {
            process.stdout.write(dir.replace(/\/$/, "") + "/\n");
            fs.mkdir(dir, cb);
        });
    });
}


function admZip2(ccrPath, successCb) {
    var o = `${global.tempDir}/tmp/zip/${ccrPath.name}/`;
	console.log('depress:--->'+ccrPath.path,'2222'+o);
	try{
		var unzipCode = DLL.DecompressZipDec(ccrPath.path, o);
		successCb(unzipCode);
	}catch (e){
		successCb('error');
	}
	
	console.log('当前返回的值是-->'+unzipCode);
	
	// TODO unzipCode
	
	
	/*
    yauzl.open(ccrPath.path, {lazyEntries: true}, function (err, zipfile) {
        if (err) {
            throw err;
        }

        zipfile.on("close", function () {
            successCb();
        });

        zipfile.readEntry();

        zipfile.on("entry", function (entry) {
            if (/\/$/.test(entry.fileName)) {
                // directory file names end with '/'
                mkdirp(o + entry.fileName, function () {
                    if (err) {
                        throw err;
                    }
                    zipfile.readEntry();
                });
            } else {
                // ensure parent directory exists
                mkdirp(o + path.dirname(entry.fileName), function () {
                    zipfile.openReadStream(entry, function (err, readStream) {
                        if (err) {
                            throw err;
                        }

                        var filter = new Transform();
                        filter._transform = function (chunk, encoding, cb) {
                            cb(null, chunk);
                        };
                        filter._flush = function (cb) {
                            cb();
                            zipfile.readEntry();
                        };

                        // pump file contents
                        var writeStream = fs.createWriteStream(o + entry.fileName);
                        readStream.pipe(filter).pipe(writeStream);
                    });
                });
            }
        });
    });
	*/
};
module.exports.admZip2 = admZip2;