diff --git a/src/util.rs b/src/util.rs index 12b0ea58..0ceb133a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -70,3 +70,31 @@ impl<'a, R: Reader> Reader for RefMutReader<'a, R> self.inner.read(buf) } } + +/// Additional integer write methods for a io::Read +pub trait WriteIntExt { + /// Write a u32 in little-endian mode + fn write_le_u32(&mut self, u32) -> io::Result<()>; + /// Write a u16 in little-endian mode + fn write_le_u16(&mut self, u16) -> io::Result<()>; +} + +impl WriteIntExt for W { + fn write_le_u32(&mut self, val: u32) -> io::Result<()> { + let mut buf = [0u8; 4]; + let v = val; + buf[0] = ((v >> 0) & 0xFF) as u8; + buf[1] = ((v >> 8) & 0xFF) as u8; + buf[2] = ((v >> 16) & 0xFF) as u8; + buf[3] = ((v >> 24) & 0xFF) as u8; + self.write_all(&buf) + } + + fn write_le_u16(&mut self, val: u16) -> io::Result<()> { + let mut buf = [0u8; 2]; + let v = val; + buf[0] = ((v >> 0) & 0xFF) as u8; + buf[1] = ((v >> 8) & 0xFF) as u8; + self.write_all(&buf) + } +} diff --git a/src/writer.rs b/src/writer.rs index b873b0d9..af1c4521 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -6,7 +6,6 @@ use crc32; use result::{ZipResult, ZipError}; use std::default::Default; use std::io; -use std::io::Seek; use std::io::prelude::*; use std::old_io::Writer; use std::mem; @@ -64,7 +63,7 @@ struct ZipWriterStats bytes_written: u64, } -impl Write for ZipWriter +impl Write for ZipWriter { fn write(&mut self, buf: &[u8]) -> io::Result { @@ -98,7 +97,7 @@ impl ZipWriterStats } } -impl ZipWriter +impl ZipWriter { /// Initializes the ZipWriter. /// @@ -135,9 +134,7 @@ impl ZipWriter header_start: header_start, data_start: 0, }; - let mut conv_w = IoConverter::new(writer); - try!(writer_spec::write_local_file_header(&mut conv_w, &file)); - let writer = conv_w.into_inner(); + try!(writer_spec::write_local_file_header(writer, &file)); let header_end = try!(writer.seek(io::SeekFrom::Current(0))); self.stats.start = header_end; @@ -168,9 +165,7 @@ impl ZipWriter file.uncompressed_size = self.stats.bytes_written; file.compressed_size = try!(writer.seek(io::SeekFrom::Current(0))) - self.stats.start; - let mut conv_w = IoConverter::new(writer); - try!(writer_spec::update_local_file_header(&mut conv_w, file)); - let writer = conv_w.into_inner(); + try!(writer_spec::update_local_file_header(writer, file)); try!(writer.seek(io::SeekFrom::End(0))); Ok(()) } @@ -194,12 +189,10 @@ impl ZipWriter let writer = self.inner.get_plain(); let central_start = try!(writer.seek(io::SeekFrom::Current(0))); - let mut conv_w = IoConverter::new(writer); for file in self.files.iter() { - try!(writer_spec::write_central_directory_header(&mut conv_w, file)); + try!(writer_spec::write_central_directory_header(writer, file)); } - let writer = conv_w.into_inner(); let central_size = try!(writer.seek(io::SeekFrom::Current(0))) - central_start; let footer = spec::CentralDirectoryEnd @@ -221,7 +214,7 @@ impl ZipWriter } #[unsafe_destructor] -impl Drop for ZipWriter +impl Drop for ZipWriter { fn drop(&mut self) { @@ -234,7 +227,7 @@ impl Drop for ZipWriter } } -impl GenericZipWriter +impl GenericZipWriter { fn switch_to(&mut self, compression: CompressionMethod) -> ZipResult<()> { diff --git a/src/writer_spec.rs b/src/writer_spec.rs index 2827fc5a..62e2540b 100644 --- a/src/writer_spec.rs +++ b/src/writer_spec.rs @@ -1,12 +1,13 @@ -use std::old_io; +use std::io; use std::io::prelude::*; use std::ascii::AsciiExt; use types::ZipFile; use result::ZipResult; use spec; use util; +use util::WriteIntExt; -pub fn write_local_file_header(writer: &mut T, file: &ZipFile) -> ZipResult<()> +pub fn write_local_file_header(writer: &mut T, file: &ZipFile) -> ZipResult<()> { try!(writer.write_le_u32(spec::LOCAL_FILE_HEADER_SIGNATURE)); try!(writer.write_le_u16(20)); @@ -27,17 +28,17 @@ pub fn write_local_file_header(writer: &mut T, file: &ZipFile) -> Zip Ok(()) } -pub fn update_local_file_header(writer: &mut T, file: &ZipFile) -> ZipResult<()> +pub fn update_local_file_header(writer: &mut T, file: &ZipFile) -> ZipResult<()> { - static CRC32_OFFSET : i64 = 14; - try!(writer.seek(file.header_start as i64 + CRC32_OFFSET, old_io::SeekSet)); + static CRC32_OFFSET : u64 = 14; + try!(writer.seek(io::SeekFrom::Start(file.header_start + CRC32_OFFSET))); try!(writer.write_le_u32(file.crc32)); try!(writer.write_le_u32(file.compressed_size as u32)); try!(writer.write_le_u32(file.uncompressed_size as u32)); Ok(()) } -pub fn write_central_directory_header(writer: &mut T, file: &ZipFile) -> ZipResult<()> +pub fn write_central_directory_header(writer: &mut T, file: &ZipFile) -> ZipResult<()> { try!(writer.write_le_u32(spec::CENTRAL_DIRECTORY_HEADER_SIGNATURE)); try!(writer.write_le_u16(0x14FF)); @@ -66,7 +67,7 @@ pub fn write_central_directory_header(writer: &mut T, file: &ZipFile) fn build_extra_field(_file: &ZipFile) -> ZipResult> { - let writer = old_io::MemWriter::new(); + let writer = Vec::new(); // Future work - Ok(writer.into_inner()) + Ok(writer) }