3

Assume the following .zip file:

unzip -l myarchive.zip 
Archive:  myarchive.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     3663  1980-00-00 00:00   sub_dir1/file1.txt
     4573  1980-00-00 00:00   sub_dir1/file2.txt
     6021  1980-00-00 00:00   sub_dir2/file1.txt
     6627  1980-00-00 00:00   file1.txt

The following command extracts the file sub_dir1/file1.txt from the .zip file when it is in the file system.

unzip -p myarchive.zip sub_dir1/file1.txt > file1.txt

But if the .zip file is in Google Drive with a shared link (e.g. the fileId is: 1234567...v4rzj),

Is it possible to make a Google Drive API query to get a specific file (e.g. sub_dir1/file1.txt) from within a .zip file?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Avner Moshkovitz
  • 1,138
  • 1
  • 18
  • 35

2 Answers2

1

I am attempting to do a similar action. Take a look at my question here. How to read file names of items in a Zipped Folder? Google App Script

This portion of the code can unzip the file on Google Drive and place it in any location you need. However it will run through the entire zip folder.

/// "var zfi" define a zip file iterator ///

while (zfi.hasNext()){                                // loops through ZIP file iterator
      var file = zfi.next();                          // every loop sets active file to next
        Logger.log("Zip Folder: %s", file.getName());
      var fileBlob = file.getBlob();                  // get file blob
      fileBlob.setContentType("application/zip");                                             
      var unZippedfile = Utilities.unzip(fileBlob);   // unzipped file iterator

  //// loops all blob elements ////
      for (i=0; i<unZippedfile.length; i++) {                                                
        var uzf = temp.createFile(unZippedfile[i]);
AaronS
  • 43
  • 6
0

Google drive is simply a file storage system it in and of itself it does not have the ability to unzip files in this manner or to check the contents of a file. The google drive api just gives you the ability to Create, update ,delete upload and download the files.

Other options.

as your unzip command works on a file stored locally on your machine. You will need to download the file from Google drive first and then run your unzip.

As you have not mentioned which programming language you are intending to use i recommend checking the documentation for examples.

This is an example using Java, you will need the authorization code as well.

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
    .executeMediaAndDownloadTo(outputStream);
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449