From 7ce0f5b21f7b112a2e84471f087cdf5c61e6d966 Mon Sep 17 00:00:00 2001 From: Mathijs van de Nes Date: Wed, 10 Sep 2014 16:40:13 +0200 Subject: [PATCH] Cleaned the code of ZipReader's constructor --- src/reader.rs | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 514ec5fc..2cd7a2c7 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -25,31 +25,24 @@ fn unsupported_zip_error(detail: &str) -> IoResult impl ZipReader { - pub fn new(inner: T) -> IoResult> + pub fn new(mut reader: T) -> IoResult> { - let mut result = ZipReader { inner: RefCell::new(inner), files: Vec::new() }; + let footer = try!(spec::CentralDirectoryEnd::find_and_parse(&mut reader)); + if footer.number_of_disks > 1 { return unsupported_zip_error("Support for multi-disk files is not implemented") } + + let directory_start = footer.central_directory_offset as i64; + let number_of_files = footer.number_of_files_on_this_disk as uint; + + let mut files = Vec::with_capacity(number_of_files); + + try!(reader.seek(directory_start, io::SeekSet)); + for i in range(0, number_of_files) { - let reader = &mut *result.inner.borrow_mut(); - let footer = try!(spec::CentralDirectoryEnd::find_and_parse(reader)); - - if footer.number_of_disks > 1 { return unsupported_zip_error("Support for multi-disk files is not implemented") } - - let directory_start = footer.central_directory_offset as i64; - let number_of_files = footer.number_of_files_on_this_disk as uint; - - let mut files = Vec::with_capacity(number_of_files); - - try!(reader.seek(directory_start, io::SeekSet)); - for i in range(0, number_of_files) - { - files.push(try!(ZipReader::parse_directory(reader))); - } - - result.files = files; + files.push(try!(ZipReader::parse_directory(&mut reader))); } - Ok(result) + Ok(ZipReader { inner: RefCell::new(reader), files: files }) } fn parse_directory(reader: &mut T) -> IoResult