Difference in size of HTML file directly downloaded from Google Drive and via Drive API - google-drive-api

I uploaded a word document on Google Drive. I am downloading the file in HTML format in following two ways.
First method: I open the file in Google Docs and then downloaded the file: File -> Download as -> Web page (.html, zipped). I unzip the tar and then I get the HTML file. Its size is 62 kB.
Second method: I used Drive API v3 to create (or upload) the word document to Google Drive. Then I exported the file to HTML format. Its size is 173 kB.
My question is why there is difference of almost three times in size of HTML file? What should be done to get the same file size (62 kB) when downloading (or exporting) using Drive API?
This is the Drive API code I am using to create and export the file.
Drive service = getDriveService();
File fileMetadata = new File();
fileMetadata.setName("Test Document");
fileMetadata.setMimeType("application/vnd.google-apps.document");
FileContent fileContent = new FileContent("application/vnd.openxmlformats-officedocument.wordprocessingml.document", new java.io.File("/home/test/test.doc"));
File createResponse = service.files().create(fileMetadata, fileContent).execute();
java.io.File parentDir = new java.io.File("/home/test/");
if (!parentDir.exists()) {
throw new IOException("Parent directory does not exists.");
}
OutputStream out = new FileOutputStream(new java.io.File(parentDir, "Test Document"));
service.files().export(createResponse.getId(), "text/html").executeAndDownloadTo(out);
Kindly help me with this issue.
Thanks.

Related

Is it possible to use the Google Drive API to get file from within a shared .zip file

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?
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]);
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);

BOX: How to download file using file url?

I want to use Box SDK to download a file. I have the file link available in local variable
var url = https://mycompany.box.com/s/c565vhytyhx5s85vjg03bgtr0h47d6nh
Currently BoxClient.FilesManager.DownloadStreamAsync(fileid) takes fileid as parameter.
How do I use url to download a file from box using Box SDK?
Use following Code to Download File From Box using box api
BoxFile file=new BoxFile(api, fileID);
FileOutputStream fOut = new FileOutputStream(file path);
URL DownloadUrl=file.getDownloadURL();
BufferedInputStream input=new BufferedInputStream(DownloadUrl.openStream());
and write above file "input" to FileOutputStream with file path where u want

Convert all xls files available in a folder into "Google Doc Spreadsheets"?

The basic way to do that is turn conversion on when uploading files into Google Drive.
Another way is to select the xls file in the folder and convert it one by one by hand.
But if one has already many xls files uploaded in a folder, it may be faster to convert them with Google Apps Script than re-uploading again the files.
In my case:
once converted, I need to delete the xls files
all xls files are below the limits : "Uploaded spreadsheet files that are converted to the Google spreadsheets format can’t be larger than 100 MB, and need to be under 400,000 cells and 256 columns per sheet."
https://support.google.com/drive/answer/37603?hl=en
Thanks in advance;)
you should use the Advanced Drive Service, file update
This service must be enabled before use but the doc is pretty clear.
EDIT (following your comments)
(sorry for the long delay, I forgot this post)
This code will convert every XLS files in your drive to Google spreadsheet format (as long as their names have an .xls extension)
You must authorize the Drive extended ressource + API console (follow the instructions from the ressource/advanced services menu, see illustration below)
function importXLS(){
var files = DriveApp.searchFiles('title contains ".xls"');// you can also use a folder as starting point and get the files in that folder... use only DriveApp method here.
while(files.hasNext()){
var xFile = files.next();
var name = xFile.getName();
if (name.indexOf('.xls')>-1){ // this check is not necessaey here because I get the files with a search but I left it in case you get the files differently...
var ID = xFile.getId();
var xBlob = xFile.getBlob();
var newFile = { title : name+'_converted',
key : ID
}
file = Drive.Files.insert(newFile, xBlob, {
convert: true
});
}
}
}

The uploaded files are not converted to Google Documents

I use Google Drive API to upload new files (https://developers.google.com/drive/v2/reference/files/insert), and I use the 'convert' parameter to convert the file in Google Document.
It worked well but today, the uploaded files are not converted.
I tried with a .doc, .docx and .ppt, and I have the same problem.
Does anyone has an idea? Is the API has been changed?
Make sure you are using the correct mime type. See code below.
Drive driveService = new Drive.Builder(...).setApplicationName(...).build();
File fileMetadata = new File();
fileMetadata.setName("file1.doc");
fileMetadata.setMimeType("application/vnd.google-apps.document");
AbstractInputStreamContent inputStream = new InputStreamContent(null,
new FileInputStream(new java.io.File("/file1.doc")));
File file = driveService.files().create(fileMetadata, in).setFields("id").execute();

Upload to deleted folder in Google Drive is not throwing exception

I have an Android app that uploads to a folder within Google Drive. I have that part working OK. Now if I delete the folder within Google Drive and then my app attempts to upload to this folder, the upload is "successful" but I cannot find the file in Google Drive.
I would expect an Exception to be thrown by Google Drive but as far as I can tell, it is not.
Is this by design? Do I need to resort to checking if the folder exists before each upload or is there another way?
Thanks
Here is the code that I am using
File body = new File();
body.setTitle(title);
body.setDescription(description);
body.setMimeType(mimeType);
// Set the parent folder.
body.setParents(Arrays.asList(new ParentReference().setId(parentId)));
// File's content.
java.io.File fileContent = new java.io.File(filename);
FileContent mediaContent = new FileContent(mimeType, fileContent);
try {
drive.files().insert(body, mediaContent).execute();
} catch (IOException e) {
e.printStackTrace();
}
The return value of the file insert method is the metadata of the inserted file. You can use that to know everything about the new file on Google Drive, including the folder(s) it is included in.