title: Unzip password protected archive and read the containing SPSS file into R
body: <p>I have a password protected zip archive containing one SPSS system data file (*.sav). And I want to unpack it and read its content into R. From what I googled, <code>Hmisc::getZip</code> seems to be the best bet. And I've tried some different approaches.</p>
  
  <p>First try was to just run the function, hoping that it would spit out the <code>*.sav</code> file in my <code>getwd()</code> location.</p>
  
  <pre><code>getZip(url = '/path/to/data.zip', 
    password = 'foo'
  )
  </code></pre>
  
  <p>But this command returns:</p>
  
  <pre><code>"unzip -p -P foo data.zip" 
  class 
  "pipe" 
  mode 
  "r" 
  text 
  "text" 
  opened 
  "closed" 
  can read 
  "yes" 
  can write 
  "yes" 
  </code></pre>
  
  <p>And after reading the help file, the <code>getZip()</code> function seems to return a file O/I pipe. So my second try was to use <code>getZip()</code> inside <code>read.spss()</code>. Like this:</p>
  
  <pre><code>data &lt;- read.spss(getZip(url = '/path/to/data.zip', 
    password = 'foo')
  )
  </code></pre>
  
  <p>But no love:</p>
  
  <pre><code>Error in read.spss(getZip(url = '/path/to/data.zip', password = 'foo')) : 
    unable to open file: 'No such file or directory'
  </code></pre>
  
  <p>When I take the command from my first try "<code>unzip -p -P foo data.zip</code>" and run it (with an added "<code>> data.sav</code>") from the command line I get the SPSS file. So something is working. My third try was to use the connection:</p>
  
  <pre><code>file_connection &lt;- getZip(url = '/path/to/data.zip', password = 'foo')
  open(file_connection, 'rb')
  data &lt;- readBin(file_connection, raw())
  close(file_connection)
  </code></pre>
  
  <p>I was hoping to save the data object to an <code>*.sav</code> file and read it back into R with the <code>read.spss()</code> function. But the data object is not an SPSS file:</p>
  
  <pre><code>&gt; data
  [1] 24
  </code></pre>
  
  <p>So... How should I successfully unpack and read the <code>*.sav</code> file from the password protected zip archive into R?</p>
  
  <pre><code>&gt; sessionInfo()
  R version 3.3.0 (2016-05-03)
  Platform: x86_64-pc-linux-gnu (64-bit)
  Running under: Ubuntu 14.04.4 LTS
  
  locale:
   [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                 
   [3] LC_TIME=en_US.UTF-8           LC_COLLATE=en_US.UTF-8       
   [5] LC_MONETARY=sv_SE.UTF-8       LC_MESSAGES=en_US.UTF-8      
   [7] LC_PAPER=sv_SE.UTF-8          LC_NAME=sv_SE.UTF-8          
   [9] LC_ADDRESS=sv_SE.UTF-8        LC_TELEPHONE=sv_SE.UTF-8     
  [11] LC_MEASUREMENT=sv_SE.UTF-8    LC_IDENTIFICATION=sv_SE.UTF-8
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
  [1] gmailr_0.7.1 rj_2.0.5-1  
  
  loaded via a namespace (and not attached):
   [1] Rcpp_0.12.5         Formula_1.2-1       cluster_2.0.4      
   [4] magrittr_1.5        splines_3.3.0       munsell_0.4.3      
   [7] rj.gd_2.0.0-1       colorspace_1.2-6    lattice_0.20-33    
  [10] R6_2.1.2            httr_1.1.0          plyr_1.8.3         
  [13] tools_3.3.0         nnet_7.3-12         grid_3.3.0         
  [16] data.table_1.9.6    gtable_0.2.0        latticeExtra_0.6-28
  [19] openssl_0.9.3       survival_2.39-4     Matrix_1.2-6       
  [22] gridExtra_2.2.1     RColorBrewer_1.1-2  ggplot2_2.1.0      
  [25] base64enc_0.1-3     acepack_1.3-3.3     rpart_4.1-10       
  [28] curl_0.9.7          scales_0.4.0        Hmisc_3.17-4       
  [31] jsonlite_0.9.20     chron_2.3-47        foreign_0.8-66  
  </code></pre>
  
  <p><strong>EDIT:</strong> OK. So I have a (not so elegant) workaround. I'm using a <code>system()</code> call with the <code>wait = TRUE</code> argument to unpack the file. Like this:</p>
  
  <pre><code>system(command = paste0('unzip -P foo ', data.zip), 
    wait = TRUE
  )
  </code></pre>
  
  <p>Then I can read the <code>*.sav</code> file into R. But I still don't understand how to use <code>Hmisc::getZip()</code></p>