r function unzip error 1 in extracting from zip file
Asked Answered
P

11

24

Environment: Windows 7 OS RStudio Version 0.99.491

I have been programming in R for about 4 months via the Coursera Data Science curriculum, but I have NEVER been successful in using the unzip function.

I've looked at the forums for hours for potential solutions, syntax problems, undefined arguments, etc., but to no avail. I eventually unzip the contents manually and proceed with the assignment, but I am tired of not knowing why it is not working.

Here are a few examples of the error:

fileName <- "StormData.zip"

unzip(fileName, exdir = mainDir,subDir)

Warning message: In unzip(fileName, exdir = mainDir, subDir) : error 1 in extracting from zip file

unzip(fileName)

Warning message: In unzip(fileName) : error 1 in extracting from zip file

unzip(fileName, "stormdata.csv")

Warning message: In unzip(fileName, "stormdata.csv") : error 1 in extracting from zip file

unzip(fileName, "stormdata.csv", list = TRUE)

Error in unzip(fileName, "stormdata.csv", list = TRUE) : zip file 'StormData.zip' cannot be opened

Any suggestions would be greatly appreciated.

Pricilla answered 14/5, 2016 at 0:33 Comment(4)
Where are you getting the zip file from?Kenway
It sounds like a permissions issue.Estrada
file is download from linkPricilla
I have no probems downloading and manually extracting the CSV file. I have full administrative privileges on my computer.Pricilla
V
9

I was getting the same error.

I changed the path --

from :

uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata"

to

uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata.zip"

and it works fine!

setwd("C:\\Users\\Sharvari\\Downloads")

uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata.zip"

unzip(uzp, exdir = "C:\\Users\\Sharvari\\Desktop\\specdata")
Vietnam answered 16/4, 2017 at 10:40 Comment(1)
With RStudio on Windows you can also use "/" instead of "\\".Miscarry
S
6

I too was getting that error 1 message when trying to unzipping a zip file. Glitch in my case was the conflict between working directory and zip file path.

My case was:

  • My working directory was like "C:/Users/SCOTT/Desktop/Training"
  • While my zip file was located in "C:/Users/SCOTT/Desktop/Training/house_consumption_data"

When I was trying to execute this:

     unzip("house_data.zip")

Possibly your file is in a different folder.

Simla answered 17/5, 2016 at 19:54 Comment(0)
M
3

I have had the same problem trying to download and unzip the same file, for the same course. And I have had problems with unzip in the past and was determined to solve it this time too.

Eventually the extension of the file turned out to be csv.bz2. And than this Extract bz2 file in R post solved my problem. After downloading the file I was able to read it directly with

stormdata <- read.csv("stormdata.zip")

without using unzip.

Milkwhite answered 15/9, 2016 at 21:32 Comment(0)
H
1

This error seems to appear whenever openXLS is unable to open the specified file. It could be a wrong name, wrong directory or the file might be encrypted or password protected

Henandchickens answered 13/9, 2017 at 22:41 Comment(0)
K
0

change your zip file format this error will appear while the zip format problems occur, look at your zip file it should be "rar" change it to "zip". the function works only for "zip" format files.

Kitty answered 1/7, 2017 at 22:30 Comment(0)
T
0

I faced the same issue. Make sure that, you specify the correct name of the file(get it from the properties of .zip file) in the following code.

file = read.table(unzip("file_name.csv.zip"), sep = ",", header = TRUE)

In my case, Was just mentioning file_name.zip and R was throwing the error.

Also, there are two functions for unzipping files in R

1) unz - to extract single element from zip file/s 2) unzip - to extract all the present elements from the .zip file

I usually prefer unzip. If you will use unz in the above code, R will throw error again.

Trstram answered 1/9, 2018 at 10:50 Comment(0)
I
0

I encountered the same error using install_course_zip' with a zip file. I followed all the instructions for the command faithfully but kept getting errors relating to the 'exdir'. I moved the zip file to various directories without success.

I finally used getwd() to get the working directory and then placed the zip file in that directory. I then was able to use the zip file name without having to use any folder structure and this worked. I still have no idea why R would not accept a different directory.

Irritative answered 10/2, 2021 at 10:58 Comment(0)
K
0

I had list of files to be unzipped and processed; I was facing same error "error 1 in extracting from zip file"

used full directory and set working directory code worked


files <- list.files(path="C:\\Users\\Tejas naik\\Documents\\land", pattern=".zip$")

out_dir<- "C:\\Users\\Tejas naik\\Documents\\input"

setwd("C:\\Users\\Tejas naik\\Documents\\land")

for (i in files) {
  #nzip(paste(out_dir,i), exdir=out_dir)
  unzip(i,exdir=out_dir)
  }
Kidney answered 11/9, 2021 at 16:2 Comment(0)
T
0

This error was happening bit differently in my case . As there was no zip file ,the issue was file was open in excel so this error was poping up .

Tafoya answered 30/3, 2022 at 8:32 Comment(0)
P
0

It's crucial to give the full name (including the path) of the zip-file to the unzip function.

So instead of file.zip, it should be C:\user\name\file.zip.

In case you're using the list.files function, one should set the full.names option to TRUE.

Pugh answered 17/5, 2022 at 7:34 Comment(0)
C
0

For me the error is fixed after I add \ backslash character to the filepath. Example: from

unzip("abc\aaa.zip")

to

unzip("abc\\aaa.zip")
Commines answered 16/12, 2022 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.