This is a GAS library for unzipping a Zip file protected by a password using Google Apps Script.
Recently, I had a situation that it is required to unzip a Zip file protected with the password. But unfortunately, in the current stage, the method of Utilities.unzip()
cannot unzip such protected files. So when I had been looking for the other workarounds, I found zlib.js
. Especially, it's unzip.min.js
. This is created for Javascript. So when I used this using Google Apps Script, it was found that it didn't work for Google Apps Script. So I prepared a wrapper script for running it with Google Apps Script. And when I created the wrapper script, I thought that under the current stage, if the protected Zip file can be unzipped, this will be useful for other users. So I created this as a GAS library.
1yeBx1bM_6T-Tt2S3e-wxSgphR2QdhM7r0YRYAUTXzDkbRIrvJdHNZT0R
- Open Script Editor. And please operate follows by click.
- -> Resource
- -> Library
- -> Input Script ID to text box. Script ID is
1yeBx1bM_6T-Tt2S3e-wxSgphR2QdhM7r0YRYAUTXzDkbRIrvJdHNZT0R
. - -> Add library
- -> Please select latest version
- -> Developer mode ON (If you don't want to use latest version, please select others.)
- -> Identifier is "
UnzipGs
". This is set under the default.
If you want to read about Libraries, please check this..
This library uses no scopes.
In the current stage, there are 2 methods.
This method unzips a Zip file. blob
is the file blob of the Zip file. The protected Zip file can be unzipped by setting the password to object
. The response value is []blob
including the file blobs.
If password
is not included in object
, the zip file is decompressed using Utilities.unzip()
.
function sample() {
var id = "###"; // File ID of Zip file.
var pass = "###"; // Password.
var blob = DriveApp.getFileById(id).getBlob();
var res = UnzipGs.unzip(blob, { password: pass }); // or UnzipGs.unzip(blob);
res.forEach(function(e) {
Logger.log(
"filename: %s, mimeType: %s, size: %s",
e.getName(),
e.getContentType(),
e.getBytes().length
);
});
}
This method retrieves the filenames in the Zip file. There are no methods for retrieving the filenames from the Zip file in Google Apps Script. So I added this. This also uses zlib.js.
function sample() {
var id = "###"; // File ID of Zip file.
var pass = "###"; // Password.
var blob = DriveApp.getFileById(id).getBlob();
var res = UnzipGs.getFilenames(blob, { password: pass }); // or UnzipGs.getFilenames(blob);
Logger.log(res);
}
About the process cost for decompressing, the cost of UnzipGs.unzip()
is much higher than that of Utilities.unzip()
.
-
For example, in my environment, when a protected Zip file with the size of 1 MB is unzipped using this library, the process time was about 60 seconds to 90 seconds.
-
On the other hand, when
Utilities.unzip()
is used for the same file without the password, the process time was about 1 second.
-
When this library is used, please be careful the file size of the Zip file and unzipped files. Because the maximum size of blob which can be used for Google Apps Script is 50 MB (52,428,800 bytes).
-
But, when the Zip file is unzipped without using
Utilities.unzip()
by Google Apps Script, the process cost becomes very high. So I think that the practical size will be required to become smaller. -
By this situation, unfortunately, I couldn't implement the method for zipping.
-
The reason of this issue is due to the process of Google Apps Script. For example, if v8 engine can be used with Google Apps Script in the future, this situation will be changed. I would like to expect this.
-
Or Google might also update
Utilities.unzip()
by including the method for decompressing the protected Zip file in the future.
- Imaya Yuta: He is the author of zlib.js.
- This library (UnzipGs) uses
unzip.min.js
ofzlib.js
. So this library is a wrapper script for runningunzip.min.js
, which is for Javascript, with Google Apps Script.
- This library (UnzipGs) uses
If you have any questions and commissions for me, feel free to contact me.
-
v1.0.0 (July 26, 2019)
- Initial release.
- September 16, 2020: When I used a sample zip file with the password, I could confirm that the script worked. But, unfortunately, it seems that
unzip.min.js
cannot be used with v8 runtime. So when this library is used, please disable V8 runtime at the script editor.