adm-zip icon indicating copy to clipboard operation
adm-zip copied to clipboard

Directory is not detect from .isDirectory

Open salsan opened this issue 4 years ago • 6 comments

I've created a simple test archive with this code


const fs = require('fs');
const path = require('path');
const Archive = require('adm-zip');
const zip = new Archive();

zip.addLocalFolder('TEST_1', 'Files/TEST_1');
zip.writeZip(/*target file name*/ "Test_1.zip");

This is archive generated immagine

This is script for read Archive

const fs = require('fs');
const path = require('path');
const Archive = require('adm-zip');

const zip = new Archive("Test_1.zip");
var zipEntries = zip.getEntries(); // an array of ZipEntry records

zipEntries.forEach(function (zipEntry) {
           console.log(zipEntry.toString());      
});

This is the result, why no one directory found ?

{
        "entryName" : "Files/TEST_1/File1.txt",
        "name" : "File1.txt",
        "comment" : "",
        "isDirectory" : false,
        "header" : {
                "made" : 2836,
                "version" : 20,
                "flags" : 0,
                "method" : STORED (0),
                "time" : Thu Jun 24 2021 20:05:22 GMT+0200 (Ora legale dell’Europa centrale),
                "crc" : 0x0,
                "compressedSize" : 0 bytes,
                "size" : 0 bytes,
                "fileNameLength" : 22,
                "extraLength" : 0 bytes,
                "commentLength" : 0 bytes,
                "diskNumStart" : 0,
                "inAttr" : 0,
                "attr" : 0,
                "offset" : 0,
                "entryHeaderSize" : 68 bytes
        },
        "compressedData" : <384 bytes buffer>
        "data" : <null>
}
{
        "entryName" : "Files/TEST_1/File2.txt",
        "name" : "File2.txt",
        "comment" : "",
        "isDirectory" : false,
        "header" : {
                "made" : 2836,
                "version" : 20,
                "flags" : 0,
                "method" : STORED (0),
                "time" : Thu Jun 24 2021 20:05:22 GMT+0200 (Ora legale dell’Europa centrale),
                "crc" : 0x0,
                "compressedSize" : 0 bytes,
                "size" : 0 bytes,
                "fileNameLength" : 22,
                "extraLength" : 0 bytes,
                "commentLength" : 0 bytes,
                "diskNumStart" : 0,
                "inAttr" : 0,
                "attr" : 0,
                "offset" : 52,
                "entryHeaderSize" : 68 bytes
        },
        "compressedData" : <384 bytes buffer>
        "data" : <null>
}
{
        "entryName" : "Files/TEST_1/File3..txt",
        "name" : "File3..txt",
        "comment" : "",
        "isDirectory" : false,
        "header" : {
                "made" : 2836,
                "version" : 20,
                "flags" : 0,
                "method" : STORED (0),
                "time" : Thu Jun 24 2021 20:05:22 GMT+0200 (Ora legale dell’Europa centrale),
                "crc" : 0x0,
                "compressedSize" : 0 bytes,
                "size" : 0 bytes,
                "fileNameLength" : 23,
                "extraLength" : 0 bytes,
                "commentLength" : 0 bytes,
                "diskNumStart" : 0,
                "inAttr" : 0,
                "attr" : 0,
                "offset" : 104,
                "entryHeaderSize" : 69 bytes
        },
        "compressedData" : <384 bytes buffer>
        "data" : <null>
}

salsan avatar Jun 24 '21 18:06 salsan

Folder "Files/TEST_1/" is attached into entryName property. It did not generate extra directory entries for this folder, since all files were in root folder. But it adds folder path into file names, folder is present but without folder entries. It should add folders as entry if you had some sub folders in that folder.

5saviahv avatar Jun 25 '21 02:06 5saviahv

Another example

const fs = require('fs');
const path = require('path');
const Archive = require('adm-zip');

// ADD directory in archive
//

const zip = new Archive();
zip.addLocalFile('file_4.txt');
zip.addLocalFolder('TEST_1', 'source');
zip.writeZip(/*target file name*/ "example.zip");

If I want extract only source , how to extract if is not present the entryName ?

{
	"entryName" : "file_4.txt",
	"name" : "file_4.txt",
	"comment" : "",
	"isDirectory" : false,
	"header" : {
		"made" : 2836,
		"version" : 20,
		"flags" : 0,
		"method" : STORED (0),
		"time" : Fri Jun 25 2021 07:05:28 GMT+0200 (Ora legale dell’Europa centrale),
		"crc" : 0x0,
		"compressedSize" : 0 bytes,
		"size" : 0 bytes,
		"fileNameLength" : 10,
		"extraLength" : 0 bytes,
		"commentLength" : 0 bytes,
		"diskNumStart" : 0,
		"inAttr" : 0,
		"attr" : 0,
		"offset" : 0,
		"entryHeaderSize" : 56 bytes
	},
	"compressedData" : <444 bytes buffer>
	"data" : <null>
}
{
	"entryName" : "source/File1.txt",
	"name" : "File1.txt",
	"comment" : "",
	"isDirectory" : false,
	"header" : {
		"made" : 2836,
		"version" : 20,
		"flags" : 0,
		"method" : STORED (0),
		"time" : Thu Jun 24 2021 20:05:22 GMT+0200 (Ora legale dell’Europa centrale),
		"crc" : 0x0,
		"compressedSize" : 0 bytes,
		"size" : 0 bytes,
		"fileNameLength" : 16,
		"extraLength" : 0 bytes,
		"commentLength" : 0 bytes,
		"diskNumStart" : 0,
		"inAttr" : 0,
		"attr" : 0,
		"offset" : 40,
		"entryHeaderSize" : 62 bytes
	},
	"compressedData" : <444 bytes buffer>
	"data" : <null>
}
{
	"entryName" : "source/File2.txt",
	"name" : "File2.txt",
	"comment" : "",
	"isDirectory" : false,
	"header" : {
		"made" : 2836,
		"version" : 20,
		"flags" : 0,
		"method" : STORED (0),
		"time" : Thu Jun 24 2021 20:05:22 GMT+0200 (Ora legale dell’Europa centrale),
		"crc" : 0x0,
		"compressedSize" : 0 bytes,
		"size" : 0 bytes,
		"fileNameLength" : 16,
		"extraLength" : 0 bytes,
		"commentLength" : 0 bytes,
		"diskNumStart" : 0,
		"inAttr" : 0,
		"attr" : 0,
		"offset" : 86,
		"entryHeaderSize" : 62 bytes
	},
	"compressedData" : <444 bytes buffer>
	"data" : <null>
}
{
	"entryName" : "source/File3..txt",
	"name" : "File3..txt",
	"comment" : "",
	"isDirectory" : false,
	"header" : {
		"made" : 2836,
		"version" : 20,
		"flags" : 0,
		"method" : STORED (0),
		"time" : Thu Jun 24 2021 20:05:22 GMT+0200 (Ora legale dell’Europa centrale),
		"crc" : 0x0,
		"compressedSize" : 0 bytes,
		"size" : 0 bytes,
		"fileNameLength" : 17,
		"extraLength" : 0 bytes,
		"commentLength" : 0 bytes,
		"diskNumStart" : 0,
		"inAttr" : 0,
		"attr" : 0,
		"offset" : 132,
		"entryHeaderSize" : 63 bytes
	},
	"compressedData" : <444 bytes buffer>
	"data" : <null>
}

salsan avatar Jun 25 '21 05:06 salsan

You are correct ... since "source" directory entry was not created you can not target it with "extractEntryTo" function.

5saviahv avatar Jun 25 '21 13:06 5saviahv

I resolve the problem with this, now directory is detect from program

const fs = require('fs');
const path = require('path');
const Archive = require('adm-zip');
const zip = new Archive();

let stats = fs.statSync("TEST_1") 
zip.addLocalFolder('TEST_1', "files/");
zip.addFile('files/', Buffer.alloc(0), "", stats);
zip.writeZip(/*target file name*/ "Test_1.zip");

salsan avatar Jun 25 '21 13:06 salsan

Yes it can resolved this way,

but are you saying some external program like "7zip" (picture above) didn't found the directories or you wasn't able target directory with adm-zip ?

5saviahv avatar Jun 25 '21 13:06 5saviahv

My problem was only with adm-zip with extractEntryTo method with directory

salsan avatar Jun 25 '21 13:06 salsan