EVOLUTION-MANAGER
Edit File: use_course_details.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Helpers to download and unpack a ZIP file</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for use_course_details {usethis}"><tr><td>use_course_details {usethis}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Helpers to download and unpack a ZIP file</h2> <h3>Description</h3> <p>Details on the internal and helper functions that power <code><a href="zip-utils.html">use_course()</a></code> and <code><a href="zip-utils.html">use_zip()</a></code>. Only <code>create_download_url()</code> is exported. </p> <h3>Usage</h3> <pre> create_download_url(url) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>url</code></td> <td> <p>a GitHub, DropBox, or Google Drive URL, as copied from a web browser.</p> </td></tr> <tr valign="top"><td><code>destdir</code></td> <td> <p>Path to existing local directory where the ZIP file will be stored. Defaults to current working directory, but note that <code><a href="zip-utils.html">use_course()</a></code> has different default behavior.</p> </td></tr> <tr valign="top"><td><code>zipfile</code></td> <td> <p>Path to local ZIP file.</p> </td></tr> </table> <h3>tidy_download()</h3> <div class="sourceCode"><pre>## function signature tidy_download(url, destdir = getwd()) # as called inside use_course() tidy_download( url, ## after post-processing with normalize_url() # conspicuous_place() = `getOption('usethis.destdir')` or desktop or home # directory or working directory destdir = destdir %||% conspicuous_place() ) </pre></div> <p>Special-purpose function to download a ZIP file and automatically determine the file name, which often determines the folder name after unpacking. Developed with DropBox and GitHub as primary targets, possibly via shortlinks. Both platforms offer a way to download an entire folder or repo as a ZIP file, with information about the original folder or repo transmitted in the <code>Content-Disposition</code> header. In the absence of this header, a filename is generated from the input URL. In either case, the filename is sanitized. Returns the path to downloaded ZIP file, invisibly. </p> <p><code>tidy_download()</code> is setup to retry after a download failure. In an interactive session, it asks for user's consent. All retries use a longer connect timeout. </p> <h4>DropBox</h4> <p>To make a folder available for ZIP download, create a shared link for it: </p> <ul> <li> <p><a href="https://help.dropbox.com/files-folders/share/view-only-access">https://help.dropbox.com/files-folders/share/view-only-access</a> </p> </li></ul> <p>A shared link will have this form: </p> <div class="sourceCode"><pre>https://www.dropbox.com/sh/12345abcde/6789wxyz?dl=0 </pre></div> <p>Replace the <code>dl=0</code> at the end with <code>dl=1</code> to create a download link: </p> <div class="sourceCode"><pre>https://www.dropbox.com/sh/12345abcde/6789wxyz?dl=1 </pre></div> <p>You can use <code>create_download_url()</code> to do this conversion. </p> <p>This download link (or a shortlink that points to it) is suitable as input for <code>tidy_download()</code>. After one or more redirections, this link will eventually lead to a download URL. For more details, see <a href="https://help.dropbox.com/files-folders/share/force-download">https://help.dropbox.com/files-folders/share/force-download</a> and <a href="https://help.dropbox.com/installs-integrations/sync-uploads/download-entire-folders">https://help.dropbox.com/installs-integrations/sync-uploads/download-entire-folders</a>. </p> <h4>GitHub</h4> <p>Click on the repo's "Clone or download" button, to reveal a "Download ZIP" button. Capture this URL, which will have this form: </p> <div class="sourceCode"><pre>https://github.com/r-lib/usethis/archive/main.zip </pre></div> <p>This download link (or a shortlink that points to it) is suitable as input for <code>tidy_download()</code>. After one or more redirections, this link will eventually lead to a download URL. Here are other links that also lead to ZIP download, albeit with a different filenaming scheme (REF could be a branch name, a tag, or a SHA): </p> <div class="sourceCode"><pre>https://github.com/github.com/r-lib/usethis/zipball/HEAD https://api.github.com/repos/r-lib/rematch2/zipball/REF https://api.github.com/repos/r-lib/rematch2/zipball/HEAD https://api.github.com/repos/r-lib/usethis/zipball/REF </pre></div> <p>You can use <code>create_download_url()</code> to create the "Download ZIP" URL from a typical GitHub browser URL. </p> <h4>Google Drive</h4> <p>To our knowledge, it is not possible to download a Google Drive folder as a ZIP archive. It is however possible to share a ZIP file stored on Google Drive. To get its URL, click on "Get the shareable link" (within the "Share" menu). This URL doesn't allow for direct download, as it's designed to be processed in a web browser first. Such a sharing link looks like: </p> <div class="sourceCode"><pre>https://drive.google.com/open?id=123456789xxyyyzzz </pre></div> <p>To be able to get the URL suitable for direct download, you need to extract the "id" element from the URL and include it in this URL format: </p> <div class="sourceCode"><pre>https://drive.google.com/uc?export=download&id=123456789xxyyyzzz </pre></div> <p>Use <code>create_download_url()</code> to perform this transformation automatically. </p> <h3>tidy_unzip()</h3> <p>Special-purpose function to unpack a ZIP file and (attempt to) create the directory structure most people want. When unpacking an archive, it is easy to get one more or one less level of nesting than you expected. </p> <p>It's especially important to finesse the directory structure here: we want the same local result when unzipping the same content from either GitHub or DropBox ZIP files, which pack things differently. Here is the intent: </p> <ul> <li><p> If the ZIP archive <code>foo.zip</code> does not contain a single top-level directory, i.e. it is packed as "loose parts", unzip into a directory named <code>foo</code>. Typical of DropBox ZIP files. </p> </li> <li><p> If the ZIP archive <code>foo.zip</code> has a single top-level directory (which, by the way, is not necessarily called "foo"), unpack into said directory. Typical of GitHub ZIP files. </p> </li></ul> <p>Returns path to the directory holding the unpacked files, invisibly. </p> <p><strong>DropBox:</strong> The ZIP files produced by DropBox are special. The file list tends to contain a spurious directory <code>"/"</code>, which we ignore during unzip. Also, if the directory is a Git repo and/or RStudio Project, we unzip-ignore various hidden files, such as <code>.RData</code>, <code>.Rhistory</code>, and those below <code style="white-space: pre;">.git/</code> and <code>.Rproj.user</code>. </p> <h3>Examples</h3> <pre> ## Not run: tidy_download("https://github.com/r-lib/rematch2/archive/main.zip") ## End(Not run) ## Not run: tidy_download("https://github.com/r-lib/rematch2/archive/main.zip") tidy_unzip("rematch2-main.zip") ## End(Not run) # GitHub create_download_url("https://github.com/r-lib/usethis") create_download_url("https://github.com/r-lib/usethis/issues") # DropBox create_download_url("https://www.dropbox.com/sh/12345abcde/6789wxyz?dl=0") # Google Drive create_download_url("https://drive.google.com/open?id=123456789xxyyyzzz") create_download_url("https://drive.google.com/open?id=123456789xxyyyzzz/view") </pre> <hr /><div style="text-align: center;">[Package <em>usethis</em> version 2.1.6 <a href="00Index.html">Index</a>]</div> </body></html>