3

OK this task seems to be really easy to do. However I spent a couple of hours without any results.

User have:

  • 7z
  • Windows
  • R

User should enter:

  • path to 7z (z7path)
  • filename

System should unpack rar into the project's root

I tried:

cmd = "C:\\Program Files (x86)\\7-Zip\\7z e D:/20140601.rar"
system(shQuote(cmd))

And..nothing happens. Please don't advise to set up PATH, it doesn't help, and this should work without it.

alexsalo
  • 1,406
  • 2
  • 14
  • 16
  • 2
    What happens when you execute this directly from the Windows shell? – Andrie Jul 08 '14 at 12:57
  • 1
    Note that this probably means that the file will be extracted in the working dir of the R session (see `getwd`), is this the directory you expect? If you want another output dir, set your working dir using `setwd`, or pass a different output location to your `system` call. In addition, 7z normally asks for a confirmation to overwrite a file, see `-y` for a way to get around that. – Paul Hiemstra Jul 08 '14 at 13:06
  • thx for comments, I found two curious things: 1. file downloaded via file.download is becoming damaged somehow 2. i need to specify -o output folder – alexsalo Jul 08 '14 at 13:15
  • 1
    first try to make the code work in a ```.bat``` file. to make sure it works. but i think in the end it will look something like ```cmd = paste("C:\\Program Files (x86)\\7-Zip\\7z e D:/20140601.rar ",getwd(), sep=" ")``` – phonixor Jul 08 '14 at 13:22
  • any tips why downloading the rar file from url returns corrupted file? Although no warnings in the process. Downloading via hand (in browser) return nice working rar. – alexsalo Jul 08 '14 at 13:33
  • 1
    File download problem is a separate question. Please read the help/manual for whatever tool you're using to download the .rar file, then post a separate question if you haven't solved it. – Carl Witthoft Jul 08 '14 at 13:57

2 Answers2

3

Ok, I finally got it.

  1. Use shell
  2. Use shQuote for surrounding path
  3. Use right keys

    z7path = shQuote('C:\\Program Files (x86)\\7-Zip\\7z')
    file = paste(getwd(), '/101-01.rar', sep = '')
    cmd = paste(z7path, ' e ', file, ' -y -o', getwd(), '/', sep='')
    shell(cmd)
    
alexsalo
  • 1,406
  • 2
  • 14
  • 16
1

I had to modify the code from the second answer, and finally it works. You can change "-ir!. -o" by "-y -o" if you want all files.

z7path = shQuote('C:\\Program Files\\7-Zip\\7z')
file = paste('"', 'D:/20140601.rar', '"',sep = '')
cmd = paste(z7path, ' e ', file, ' -ir!*.* -o', '"', getwd(), '"', sep='')

system(cmd)