Buffering IO when modifying a zip archive #349
Replies: 3 comments
-
A slightly more flexible hack might be struct BufReadWrite<T> {
r: BufReader<T>,
w: BufWriter<T>,
}
impl BufReadWrite {
pub fn new(data: T) -> Self {
Self {
r: BufReader::new(data.clone()),
w: BufWriter::new(data),
}
}
}
// btw, in the case of a `File` I think the `Read` and `Write` implementations ought
// to flush each other's buffers since they're moving the underlying cursor. This is
// where the hack starts to show its cracks :D
let datasource = BufReadWriter::new(&my_file); // references are `Clone` However there is no great standard library type here. The most straightforward solution is reading your archive into memory before fiddling with it. Where that's not an option, a custom type will need to be implemented that properly provides buffered read and write. If you'd like a hand with that, let me know and I'll write a gist 😄 . I'm going to leave the issue open as there's a possibility we can solve this by relaxing the generic bound and providing some way to switch out the reader type for a writer type. Don't get your hopes up though! As always this is trivial with the new API 😛 I'll be pushing an alpha version as soon as I can once I've finished my current contract let mut writable_archive = zip::Archive::open(io::BufReader::new(file))?
// This is what's tough to implement at the moment - `ZipArchive` and `ZipWriter` aren't so easy to convert between
.map_data(|b| io::BufWriter::new(b.into_inner())); |
Beta Was this translation helpful? Give feedback.
-
The modified approach would probably work for my use case. Its downside is that it requires the
Note that my
It's not so much a case of needing a hand with the implementation, but of having a "blessed" implementation, either exposed by the library or just provided in the examples folder, which is used by others and updated as issues are found. |
Beta Was this translation helpful? Give feedback.
-
The It will be tricky to massage the |
Beta Was this translation helpful? Give feedback.
-
I don't know what type to pass to
ZipWriter::new_append()
. Specifically:ZipArcihve::new()
with a type that implementsRead
andSeek
, such asBufReader<File>
.ZipWriter::new()
with a type that implementsWrite
andSeek
, such asBufWriter<File>
.ZipWriter::new_append()
with... what type?The problem is that
new_append()
requires its argument to implement all ofRead
,Write
, andSeek
, and no buffering IO wrapper provided by the standard library satisfies those bounds. Just giving itFile
will work, but won't do any buffering, which is not something you'd want to use in production. External crates such as bufstream don't support seeking. Examples don't appear to show how to invokeZipWriter::new_append()
, and tests only call it withio::Cursor<Vec<u8>>
.Currently I am using this
hackhelper to achieve this. Is there an easier way?Beta Was this translation helpful? Give feedback.
All reactions