1

The purpose is to extract the contents of a .sketch file.

I have a file with the name myfile.sketch. On renaming the file extension to myfile.zip and extracting the same in Finder, I'm able view the files within. I tried doing the same on the server using Node.js by renaming the file extension to .zip. I wasn't able to extract the files, rather I got some ZIP files within the files.

var oldPath = __dirname+'/uploads/myfile.sketch',
newPath = __dirname+'/uploads/myfile.zip';

fs.rename(oldPath, newPath, function (err) {
console.log('rename callback ', err);
});

Is it possible to extract a non-ZIP file using frameworks like JSzip?

2
  • Just to confirm I understand right, you're asking about how to read the contents that are inside of a .sketch file? Isn't a .sketch file an image-based format? Commented Dec 27, 2017 at 7:27
  • .sketch file has a package of contents in it.. folders, images and json files within it Commented Dec 27, 2017 at 7:33

1 Answer 1

2

As a .sketch file is essentially a ZIP file, the extension does not matter. Any tool that is capable of unpacking a ZIP file will work.

You can verify this with the file command:

$ file myfile.sketch
myfile.sketch: Zip archive data, at least v1.0 to extract

As you are working on the server already, there is nothing stopping you from just using the OS's command line tools like unzip.

Like this:

const util = require('util');
const exec = util.promisify(require('child_process').exec);

async function unzip() {
  const filename = 'myfile.sketch'
  const { stdout, stderr } = await exec('unzip ' + filename);
  console.log('stdout:', stdout);
  console.log('stderr:', stderr);
}

unzip();

Doing it with JSZip is straight-forward as well:

var fs = require('fs');
var JSZip = require('jszip');

new JSZip.external.Promise(function (resolve, reject) {
  fs.readFile('myfile.sketch', function(err, data) {
    if (err) {
      reject(e);
    } else {
      resolve(data);
    }
  });
}).then(function (data) {
  return JSZip.loadAsync(data);
})
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks this works.. where should I mention the directory for the file to be extracted? Now I have the files extracted to the root folder :(
You can get all command line options by either running unzip -h or man unzip. The argument you are looking for is -d.
If you feel that my answer helped you, you could accept my answer.
@herrbischoff I am tyring your jsZip solution to extract the Sketch file, but I am not sure where to output it's unzipped/extracted content. I have see the jsZip official API but can't find how to finally output the final content. I can see the bugger output however in console when do console.log(), but don't know how to output it to any folder.
@MuhammadHannan: fs.write() / fs.writeFile()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.