1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
//! Advanced [`.npz`] file format support for [`ndarray`].
//!
//! # Accessing [`.npy`] Files
//!
//! * See [`ndarray_npy`].
//!
//! # Accessing [`.npz`] Files
//!
//! * Reading: [`NpzReader`]
//! * Writing: [`NpzWriter`]
//! * Immutable viewing (primarily for use with memory-mapped files):
//! * [`NpzView`] providing an [`NpyView`] for each uncompressed [`.npy`] file within
//! the archive
//! * Mutable viewing (primarily for use with memory-mapped files):
//! * [`NpzViewMut`] providing an [`NpyViewMut`] for each uncompressed [`.npy`] file within
//! the archive
//!
//! [`.npy`]: https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html
//! [`.npz`]: https://numpy.org/doc/stable/reference/generated/numpy.savez.html
//!
//! # Features
//!
//! Both features are enabled by default.
//!
//! * `compressed`: Enables zip archives with *deflate* compression.
//! * `num-complex-0_4`: Enables complex element types of crate `num-complex`.
#![forbid(unsafe_code)]
#![deny(
missing_docs,
rustdoc::broken_intra_doc_links,
rustdoc::missing_crate_level_docs
)]
#![allow(clippy::tabs_in_doc_comments)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
// [`NpzReader`] and [`NpzWriter`] are derivative works of [`ndarray_npy`].
pub use ndarray;
pub use ndarray_npy;
use ndarray::{
prelude::*,
{Data, DataOwned},
};
use ndarray_npy::{
ReadNpyError, ReadNpyExt, ReadableElement, ViewElement, ViewMutElement, ViewMutNpyExt,
ViewNpyError, ViewNpyExt, WritableElement, WriteNpyError, WriteNpyExt,
};
use std::{
collections::{BTreeMap, HashMap, HashSet},
error::Error,
fmt,
io::{self, BufWriter, Cursor, Read, Seek, Write},
ops::Range,
};
use zip::{
result::ZipError,
write::SimpleFileOptions,
{CompressionMethod, ZipArchive, ZipWriter},
};
/// An error writing a `.npz` file.
#[derive(Debug)]
pub enum WriteNpzError {
/// An error caused by the zip file.
Zip(ZipError),
/// An error caused by writing an inner `.npy` file.
Npy(WriteNpyError),
}
impl Error for WriteNpzError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
WriteNpzError::Zip(err) => Some(err),
WriteNpzError::Npy(err) => Some(err),
}
}
}
impl fmt::Display for WriteNpzError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WriteNpzError::Zip(err) => write!(f, "zip file error: {err}"),
WriteNpzError::Npy(err) => write!(f, "error writing npy file to npz archive: {err}"),
}
}
}
impl From<ZipError> for WriteNpzError {
fn from(err: ZipError) -> WriteNpzError {
WriteNpzError::Zip(err)
}
}
impl From<WriteNpyError> for WriteNpzError {
fn from(err: WriteNpyError) -> WriteNpzError {
WriteNpzError::Npy(err)
}
}
/// Writer for `.npz` files.
///
/// Note that the inner [`ZipWriter`] is wrapped in a [`BufWriter`] when
/// writing each array with [`.add_array()`](NpzWriter::add_array). If desired,
/// you could additionally buffer the innermost writer (e.g. the
/// [`File`](std::fs::File) when writing to a file) by wrapping it in a
/// [`BufWriter`]. This may be somewhat beneficial if the arrays are large and
/// have non-standard layouts but may decrease performance if the arrays have
/// standard or Fortran layout, so it's not recommended without testing to
/// compare.
///
/// # Example
///
/// ```no_run
/// use ndarray_npz::{
/// ndarray::{array, aview0, Array1, Array2},
/// NpzWriter,
/// };
/// use std::fs::File;
///
/// let mut npz = NpzWriter::new(File::create("arrays.npz")?);
/// let a: Array2<i32> = array![[1, 2, 3], [4, 5, 6]];
/// let b: Array1<i32> = array![7, 8, 9];
/// npz.add_array("a", &a)?;
/// npz.add_array("b", &b)?;
/// npz.add_array("c", &aview0(&10))?;
/// npz.finish()?;
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
pub struct NpzWriter<W: Write + Seek> {
zip: ZipWriter<W>,
options: SimpleFileOptions,
}
impl<W: Write + Seek> NpzWriter<W> {
/// Creates a new `.npz` file without compression. See [`numpy.savez`].
///
/// Ensures `.npy` files are 64-byte aligned for memory-mapping via [`NpzView`]/[`NpzViewMut`].
///
/// [`numpy.savez`]: https://numpy.org/doc/stable/reference/generated/numpy.savez.html
#[must_use]
pub fn new(writer: W) -> NpzWriter<W> {
NpzWriter {
zip: ZipWriter::new(writer),
options: SimpleFileOptions::default()
.with_alignment(64)
.compression_method(CompressionMethod::Stored),
}
}
/// Creates a new `.npz` file with compression. See [`numpy.savez_compressed`].
///
/// [`numpy.savez_compressed`]: https://numpy.org/doc/stable/reference/generated/numpy.savez_compressed.html
#[cfg(feature = "compressed")]
#[must_use]
pub fn new_compressed(writer: W) -> NpzWriter<W> {
NpzWriter {
zip: ZipWriter::new(writer),
options: SimpleFileOptions::default().compression_method(CompressionMethod::Deflated),
}
}
/// Adds an array with the specified `name` to the `.npz` file.
///
/// To write a scalar value, create a zero-dimensional array using [`arr0`] or [`aview0`].
///
/// # Errors
///
/// Adding an array can fail with [`WriteNpyError`].
pub fn add_array<N, S, D>(
&mut self,
name: N,
array: &ArrayBase<S, D>,
) -> Result<(), WriteNpzError>
where
N: Into<String>,
S::Elem: WritableElement,
S: Data,
D: Dimension,
{
self.zip.start_file(name.into(), self.options)?;
array.write_npy(BufWriter::new(&mut self.zip))?;
Ok(())
}
/// Calls [`.finish()`](ZipWriter::finish) on the zip file and
/// [`.flush()`](Write::flush) on the writer, and then returns the writer.
///
/// This finishes writing the remaining zip structures and flushes the
/// writer. While dropping will automatically attempt to finish the zip
/// file and (for writers that flush on drop, such as [`BufWriter`]) flush
/// the writer, any errors that occur during drop will be silently ignored.
/// So, it's necessary to call `.finish()` to properly handle errors.
///
/// # Errors
///
/// Finishing the zip archive can fail with [`ZipError`].
pub fn finish(self) -> Result<W, WriteNpzError> {
let mut writer = self.zip.finish()?;
writer.flush().map_err(ZipError::from)?;
Ok(writer)
}
}
/// An error reading a `.npz` file.
#[derive(Debug)]
pub enum ReadNpzError {
/// An error caused by the zip archive.
Zip(ZipError),
/// An error caused by reading an inner `.npy` file.
Npy(ReadNpyError),
}
impl Error for ReadNpzError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ReadNpzError::Zip(err) => Some(err),
ReadNpzError::Npy(err) => Some(err),
}
}
}
impl fmt::Display for ReadNpzError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ReadNpzError::Zip(err) => write!(f, "zip file error: {err}"),
ReadNpzError::Npy(err) => write!(f, "error reading npy file in npz archive: {err}"),
}
}
}
impl From<ZipError> for ReadNpzError {
fn from(err: ZipError) -> ReadNpzError {
ReadNpzError::Zip(err)
}
}
impl From<ReadNpyError> for ReadNpzError {
fn from(err: ReadNpyError) -> ReadNpzError {
ReadNpzError::Npy(err)
}
}
/// Reader for `.npz` files.
///
/// # Example
///
/// ```no_run
/// use ndarray_npz::{
/// ndarray::{Array1, Array2},
/// NpzReader,
/// };
/// use std::fs::File;
///
/// let mut npz = NpzReader::new(File::open("arrays.npz")?)?;
/// let a: Array2<i32> = npz.by_name("a")?;
/// let b: Array1<i32> = npz.by_name("b")?;
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
pub struct NpzReader<R: Read + Seek> {
zip: ZipArchive<R>,
}
impl<R: Read + Seek> NpzReader<R> {
/// Creates a new `.npz` file reader.
///
/// # Errors
///
/// Reading a zip archive can fail with [`ZipError`].
pub fn new(reader: R) -> Result<NpzReader<R>, ReadNpzError> {
Ok(NpzReader {
zip: ZipArchive::new(reader)?,
})
}
/// Returns `true` iff the `.npz` file doesn't contain any arrays.
#[must_use]
pub fn is_empty(&self) -> bool {
self.zip.len() == 0
}
/// Returns the number of arrays in the `.npz` file.
#[must_use]
pub fn len(&self) -> usize {
self.zip.len()
}
/// Returns the names of all of the arrays in the file.
///
/// # Errors
///
/// Reading a zip archive can fail with [`ZipError`].
pub fn names(&mut self) -> Result<Vec<String>, ReadNpzError> {
Ok((0..self.zip.len())
.map(|i| Ok(self.zip.by_index(i)?.name().to_owned()))
.collect::<Result<_, ZipError>>()?)
}
/// Reads an array by name.
///
/// # Errors
///
/// Reading an array from an archive can fail with [`ReadNpyError`] or [`ZipError`].
pub fn by_name<S, D>(&mut self, name: &str) -> Result<ArrayBase<S, D>, ReadNpzError>
where
S::Elem: ReadableElement,
S: DataOwned,
D: Dimension,
{
Ok(ArrayBase::<S, D>::read_npy(self.zip.by_name(name)?)?)
}
/// Reads an array by index in the `.npz` file.
///
/// # Errors
///
/// Reading an array from an archive can fail with [`ReadNpyError`] or [`ZipError`].
pub fn by_index<S, D>(&mut self, index: usize) -> Result<ArrayBase<S, D>, ReadNpzError>
where
S::Elem: ReadableElement,
S: DataOwned,
D: Dimension,
{
Ok(ArrayBase::<S, D>::read_npy(self.zip.by_index(index)?)?)
}
}
/// An error viewing a `.npz` file.
#[derive(Debug)]
#[non_exhaustive]
pub enum ViewNpzError {
/// An error caused by the zip archive.
Zip(ZipError),
/// An error caused by viewing an inner `.npy` file.
Npy(ViewNpyError),
/// A mutable `.npy` file view has already been moved out of its `.npz` file view.
MovedNpyViewMut,
/// Directories cannot be viewed.
Directory,
/// Compressed files cannot be viewed.
CompressedFile,
/// Encrypted files cannot be viewed.
EncryptedFile,
}
impl Error for ViewNpzError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
ViewNpzError::Zip(err) => Some(err),
ViewNpzError::Npy(err) => Some(err),
ViewNpzError::MovedNpyViewMut
| ViewNpzError::Directory
| ViewNpzError::CompressedFile
| ViewNpzError::EncryptedFile => None,
}
}
}
impl fmt::Display for ViewNpzError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ViewNpzError::Zip(err) => write!(f, "zip file error: {err}"),
ViewNpzError::Npy(err) => write!(f, "error viewing npy file in npz archive: {err}"),
ViewNpzError::MovedNpyViewMut => write!(
f,
"mutable npy file view already moved out of npz file view"
),
ViewNpzError::Directory => write!(f, "directories cannot be viewed"),
ViewNpzError::CompressedFile => write!(f, "compressed files cannot be viewed"),
ViewNpzError::EncryptedFile => write!(f, "encrypted files cannot be viewed"),
}
}
}
impl From<ZipError> for ViewNpzError {
fn from(err: ZipError) -> ViewNpzError {
ViewNpzError::Zip(err)
}
}
impl From<ViewNpyError> for ViewNpzError {
fn from(err: ViewNpyError) -> ViewNpzError {
ViewNpzError::Npy(err)
}
}
/// Immutable view for memory-mapped `.npz` files.
///
/// The primary use-case for this is viewing `.npy` files within a memory-mapped
/// `.npz` archive.
///
/// # Notes
///
/// - For types for which not all bit patterns are valid, such as `bool`, the
/// implementation iterates over all of the elements when creating the view
/// to ensure they have a valid bit pattern.
/// - The data in the buffer containing an `.npz` archive must be properly
/// aligned for its `.npy` file with the maximum alignment requirement for its
/// element type. Typically, this should not be a concern for memory-mapped
/// files (unless an option like `MAP_FIXED` is used), since memory mappings
/// are usually aligned to a page boundary.
/// - The `.npy` files within the `.npz` archive must be properly aligned for
/// their element type. Archives not created by this crate can be aligned with
/// the help of the CLI tool [`rezip`] as in `rezip in.npz -o out.npz`.
///
/// [`rezip`]: https://crates.io/crates/rezip
///
/// # Example
///
/// This is an example of opening an immutably memory-mapped `.npz` archive as
/// an [`NpzView`] providing an [`NpyView`] for each non-compressed and
/// non-encrypted `.npy` file within the archive which can be accessed via
/// [`NpyView::view`] as immutable [`ArrayView`].
///
/// This example uses the [`memmap2`](https://crates.io/crates/memmap2) crate
/// because that appears to be the best-maintained memory-mapping crate at the
/// moment, but [`Self::new`] takes a `&mut [u8]` instead of a file so that you
/// can use the memory-mapping crate you're most comfortable with.
///
/// ```
/// # if !cfg!(miri) { // Miri doesn't support mmap.
/// use std::fs::OpenOptions;
///
/// use memmap2::MmapOptions;
/// use ndarray::Ix1;
/// use ndarray_npz::{NpzView, ViewNpzError};
///
/// // Open `.npz` archive of non-compressed and non-encrypted `.npy` files in
/// // native endian.
/// #[cfg(target_endian = "little")]
/// let file = OpenOptions::new()
/// .read(true)
/// .open("tests/examples_little_endian_64_byte_aligned.npz")
/// .unwrap();
/// #[cfg(target_endian = "big")]
/// let file = OpenOptions::new()
/// .read(true)
/// .open("tests/examples_big_endian_64_byte_aligned.npz")
/// .unwrap();
/// // Memory-map `.npz` archive of 64-byte aligned `.npy` files.
/// let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
/// let npz = NpzView::new(&mmap)?;
/// // List non-compressed and non-encrypted files only.
/// for npy in npz.names() {
/// println!("{}", npy);
/// }
/// // Get immutable `.npy` views.
/// let mut x_npy_view = npz.by_name("i64.npy")?;
/// let mut y_npy_view = npz.by_name("f64.npy")?;
/// // Optionally verify CRC-32 checksums.
/// x_npy_view.verify()?;
/// y_npy_view.verify()?;
/// // Get and print immutable `ArrayView`s.
/// let x_array_view = x_npy_view.view::<i64, Ix1>()?;
/// let y_array_view = y_npy_view.view::<f64, Ix1>()?;
/// println!("{}", x_array_view);
/// println!("{}", y_array_view);
/// # }
/// # Ok::<(), ndarray_npz::ViewNpzError>(())
/// ```
#[derive(Debug, Clone)]
pub struct NpzView<'a> {
files: HashMap<usize, NpyView<'a>>,
names: HashMap<String, usize>,
directory_names: HashSet<String>,
compressed_names: HashSet<String>,
encrypted_names: HashSet<String>,
}
impl<'a> NpzView<'a> {
/// Creates a new immutable view of a memory-mapped `.npz` file.
///
/// # Errors
///
/// Viewing an archive can fail with [`ZipError`].
pub fn new(bytes: &'a [u8]) -> Result<Self, ViewNpzError> {
let mut zip = ZipArchive::new(Cursor::new(bytes))?;
let mut archive = Self {
files: HashMap::new(),
names: HashMap::new(),
directory_names: HashSet::new(),
compressed_names: HashSet::new(),
encrypted_names: zip.file_names().map(From::from).collect(),
};
// Initially assume all files to be encrypted.
let mut index = 0;
for zip_index in 0..zip.len() {
// Skip encrypted files.
let file = match zip.by_index(zip_index) {
Err(ZipError::UnsupportedArchive(ZipError::PASSWORD_REQUIRED)) => continue,
Err(err) => return Err(err.into()),
Ok(file) => file,
};
// File name of non-encrypted file.
let name = file.name().to_string();
// Remove file name from encrypted files.
archive.encrypted_names.remove(&name);
// Skip directories and compressed files.
if file.is_dir() {
archive.directory_names.insert(name);
continue;
}
if file.compression() != CompressionMethod::Stored {
archive.compressed_names.insert(name);
continue;
}
// Store file index by file names.
archive.names.insert(name, index);
let file = NpyView {
data: slice_at(bytes, file.data_start(), 0..file.size())?,
central_crc32: slice_at(bytes, file.central_header_start(), 16..20)
.map(as_array_ref)?,
status: ChecksumStatus::default(),
};
// Store file view by file index.
archive.files.insert(index, file);
// Increment index of non-compressed and non-encrypted files.
index += 1;
}
Ok(archive)
}
/// Returns `true` iff the `.npz` file doesn't contain any viewable arrays.
///
/// Viewable arrays are neither directories, nor compressed, nor encrypted.
#[must_use]
pub fn is_empty(&self) -> bool {
self.names.is_empty()
}
/// Returns the number of viewable arrays in the `.npz` file.
///
/// Viewable arrays are neither directories, nor compressed, nor encrypted.
#[must_use]
pub fn len(&self) -> usize {
self.names.len()
}
/// Returns the names of all of the viewable arrays in the `.npz` file.
///
/// Viewable arrays are neither directories, nor compressed, nor encrypted.
pub fn names(&self) -> impl Iterator<Item = &str> {
self.names.keys().map(String::as_str)
}
/// Returns the names of all of the directories in the `.npz` file.
pub fn directory_names(&self) -> impl Iterator<Item = &str> {
self.directory_names.iter().map(String::as_str)
}
/// Returns the names of all of the compressed files in the `.npz` file.
pub fn compressed_names(&self) -> impl Iterator<Item = &str> {
self.compressed_names.iter().map(String::as_str)
}
/// Returns the names of all of the encrypted files in the `.npz` file.
pub fn encrypted_names(&self) -> impl Iterator<Item = &str> {
self.encrypted_names.iter().map(String::as_str)
}
/// Returns an immutable `.npy` file view by name.
///
/// # Errors
///
/// Viewing an `.npy` file can fail with [`ViewNpyError`]. Trying to view a directory,
/// compressed file, or encrypted file, fails with [`ViewNpzError::Directory`],
/// [`ViewNpzError::CompressedFile`], or [`ViewNpzError::CompressedFile`]. Fails with
/// [`ZipError::FileNotFound`] if the `name` is not found.
pub fn by_name(&self, name: &str) -> Result<NpyView<'a>, ViewNpzError> {
self.by_index(self.names.get(name).copied().ok_or_else(|| {
if self.directory_names.contains(name) {
ViewNpzError::Directory
} else if self.compressed_names.contains(name) {
ViewNpzError::CompressedFile
} else if self.encrypted_names.contains(name) {
ViewNpzError::EncryptedFile
} else {
ZipError::FileNotFound.into()
}
})?)
}
/// Returns an immutable `.npy` file view by index in `0..len()`.
///
/// The index **does not** necessarily correspond to the index of the zip archive as
/// directories, compressed files, and encrypted files are skipped.
///
/// # Errors
///
/// Viewing an `.npy` file can fail with [`ViewNpyError`]. Fails with [`ZipError::FileNotFound`]
/// if the `index` is not found.
pub fn by_index(&self, index: usize) -> Result<NpyView<'a>, ViewNpzError> {
self.files
.get(&index)
.copied()
.ok_or_else(|| ZipError::FileNotFound.into())
}
}
/// Immutable view of memory-mapped `.npy` files within an `.npz` file.
///
/// Does **not** automatically [verify](`Self::verify`) CRC-32 checksum.
#[derive(Debug, Clone, Copy)]
pub struct NpyView<'a> {
data: &'a [u8],
central_crc32: &'a [u8; 4],
status: ChecksumStatus,
}
impl<'a> NpyView<'a> {
/// CRC-32 checksum status.
#[must_use]
pub fn status(&self) -> ChecksumStatus {
self.status
}
/// Verifies and returns CRC-32 checksum by reading the whole array.
///
/// Changes checksum [`status`](`Self::status()`) to [`Outdated`](`ChecksumStatus::Outdated`)
/// if invalid or to [`Correct`](`ChecksumStatus::Correct`) if valid.
///
/// # Errors
///
/// Fails with [`ZipError::Io`] if the checksum is invalid.
pub fn verify(&mut self) -> Result<u32, ViewNpzError> {
self.status = ChecksumStatus::Outdated;
// Like the `zip` crate, verify only against central CRC-32.
let crc32 = crc32_verify(self.data, *self.central_crc32)?;
self.status = ChecksumStatus::Correct;
Ok(crc32)
}
/// Returns an immutable view of a memory-mapped `.npy` file.
///
/// Iterates over `bool` array to ensure `0x00`/`0x01` values.
///
/// # Errors
///
/// Viewing an `.npy` file can fail with [`ViewNpyError`].
pub fn view<A, D>(&self) -> Result<ArrayView<A, D>, ViewNpzError>
where
A: ViewElement,
D: Dimension,
{
Ok(ArrayView::view_npy(self.data)?)
}
}
/// Mutable view for memory-mapped `.npz` files.
///
/// The primary use-case for this is modifying `.npy` files within a
/// memory-mapped `.npz` archive. Modifying the elements in the view will modify
/// the file. Modifying the shape/strides of the view will *not* modify the
/// shape/strides of the array in the file.
///
/// # Notes
///
/// - For types for which not all bit patterns are valid, such as `bool`, the
/// implementation iterates over all of the elements when creating the view
/// to ensure they have a valid bit pattern.
/// - The data in the buffer containing an `.npz` archive must be properly
/// aligned for its `.npy` file with the maximum alignment requirement for its
/// element type. Typically, this should not be a concern for memory-mapped
/// files (unless an option like `MAP_FIXED` is used), since memory mappings
/// are usually aligned to a page boundary.
/// - The `.npy` files within the `.npz` archive must be properly aligned for
/// their element type. Archives not created by this crate can be aligned with
/// the help of the CLI tool [`rezip`] as in `rezip in.npz -o out.npz`.
///
/// [`rezip`]: https://crates.io/crates/rezip
///
/// # Example
///
/// This is an example of opening a mutably memory-mapped `.npz` archive as an
/// [`NpzViewMut`] providing an [`NpyViewMut`] for each non-compressed and
/// non-encrypted `.npy` file within the archive which can be accessed via
/// [`NpyViewMut::view`] as immutable [`ArrayView`] or via
/// [`NpyViewMut::view_mut`] as mutable [`ArrayViewMut`]. Changes to the data in
/// the view will modify the underlying file within the archive.
///
/// This example uses the [`memmap2`](https://crates.io/crates/memmap2) crate
/// because that appears to be the best-maintained memory-mapping crate at the
/// moment, but [`Self::new`] takes a `&mut [u8]` instead of a file so that you
/// can use the memory-mapping crate you're most comfortable with.
///
/// # Example
///
/// ```
/// # if !cfg!(miri) { // Miri doesn't support mmap.
/// use std::fs::OpenOptions;
///
/// use memmap2::MmapOptions;
/// use ndarray::Ix1;
/// use ndarray_npz::{NpzViewMut, ViewNpzError};
///
/// // Open `.npz` archive of non-compressed and non-encrypted `.npy` files in
/// // native endian.
/// #[cfg(target_endian = "little")]
/// let mut file = OpenOptions::new()
/// .read(true)
/// .write(true)
/// .open("tests/examples_little_endian_64_byte_aligned.npz")
/// .unwrap();
/// #[cfg(target_endian = "big")]
/// let mut file = OpenOptions::new()
/// .read(true)
/// .write(true)
/// .open("tests/examples_big_endian_64_byte_aligned.npz")
/// .unwrap();
/// // Memory-map `.npz` archive of 64-byte aligned `.npy` files.
/// let mut mmap = unsafe { MmapOptions::new().map_mut(&file).unwrap() };
/// let mut npz = NpzViewMut::new(&mut mmap)?;
/// // List non-compressed and non-encrypted files only.
/// for npy in npz.names() {
/// println!("{}", npy);
/// }
/// // Get mutable `.npy` views of both arrays at the same time.
/// let mut x_npy_view_mut = npz.by_name("i64.npy")?;
/// let mut y_npy_view_mut = npz.by_name("f64.npy")?;
/// // Optionally verify CRC-32 checksums.
/// x_npy_view_mut.verify()?;
/// y_npy_view_mut.verify()?;
/// // Get and print mutable `ArrayViewMut`s.
/// let x_array_view_mut = x_npy_view_mut.view_mut::<i64, Ix1>()?;
/// let y_array_view_mut = y_npy_view_mut.view_mut::<f64, Ix1>()?;
/// println!("{}", x_array_view_mut);
/// println!("{}", y_array_view_mut);
/// // Update CRC-32 checksums after changes. Automatically updated on `drop()`
/// // if outdated.
/// x_npy_view_mut.update();
/// y_npy_view_mut.update();
/// # }
/// # Ok::<(), ndarray_npz::ViewNpzError>(())
/// ```
#[derive(Debug)]
pub struct NpzViewMut<'a> {
files: HashMap<usize, NpyViewMut<'a>>,
names: HashMap<String, usize>,
directory_names: HashSet<String>,
compressed_names: HashSet<String>,
encrypted_names: HashSet<String>,
}
impl<'a> NpzViewMut<'a> {
/// Creates a new mutable view of a memory-mapped `.npz` file.
///
/// # Errors
///
/// Viewing an archive can fail with [`ZipError`].
pub fn new(mut bytes: &'a mut [u8]) -> Result<Self, ViewNpzError> {
let mut zip = ZipArchive::new(Cursor::new(&bytes))?;
let mut archive = Self {
files: HashMap::new(),
names: HashMap::new(),
directory_names: HashSet::new(),
compressed_names: HashSet::new(),
encrypted_names: zip.file_names().map(From::from).collect(),
};
// Initially assume all files to be encrypted.
let mut ranges = HashMap::new();
let mut splits = BTreeMap::new();
let mut index = 0;
for zip_index in 0..zip.len() {
// Skip encrypted files.
let file = match zip.by_index(zip_index) {
Err(ZipError::UnsupportedArchive(ZipError::PASSWORD_REQUIRED)) => continue,
Err(err) => return Err(err.into()),
Ok(file) => file,
};
// File name of non-encrypted file.
let name = file.name().to_string();
// Remove file name from encrypted files.
archive.encrypted_names.remove(&name);
// Skip directories and compressed files.
if file.is_dir() {
archive.directory_names.insert(name);
continue;
}
if file.compression() != CompressionMethod::Stored {
archive.compressed_names.insert(name);
continue;
}
// Skip directories and compressed files.
if file.is_dir() || file.compression() != CompressionMethod::Stored {
continue;
}
// Store file index by file names.
archive.names.insert(name, index);
// Get data range.
let data_range = range_at(file.data_start(), 0..file.size())?;
// Get central general purpose bit flag range.
let central_flag_range = range_at(file.central_header_start(), 8..10)?;
// Parse central general purpose bit flag range.
let central_flag = u16_at(bytes, central_flag_range);
// Get central CRC-32 range.
let central_crc32_range = range_at(file.central_header_start(), 16..20)?;
// Whether local CRC-32 is located in header or data descriptor.
let use_data_descriptor = central_flag & (1 << 3) != 0;
// Get local CRC-32 range.
let crc32_range = if use_data_descriptor {
// Get local CRC-32 range in data descriptor.
let crc32_range = range_at(data_range.end, 0..4)?;
// Parse local CRC-32.
let crc32 = u32_at(bytes, crc32_range.clone());
// Whether local CRC-32 equals optional data descriptor signature.
if crc32 == 0x0807_4b50 {
// Parse central CRC-32.
let central_crc32 = u32_at(bytes, central_crc32_range.clone());
// Whether CRC-32 coincides with data descriptor signature.
if crc32 == central_crc32 {
return Err(ZipError::InvalidArchive(
"Ambiguous CRC-32 location in data descriptor",
)
.into());
}
// Skip data descriptor signature and get local CRC-32 range in data descriptor.
range_at(data_range.end, 4..8)?
} else {
crc32_range
}
} else {
// Get local CRC-32 range in header.
range_at(file.header_start(), 14..18)?
};
// Sort ranges by their starts.
splits.insert(crc32_range.start, crc32_range.end);
splits.insert(data_range.start, data_range.end);
splits.insert(central_crc32_range.start, central_crc32_range.end);
// Store ranges by file index.
ranges.insert(index, (data_range, crc32_range, central_crc32_range));
// Increment index of non-compressed and non-encrypted files.
index += 1;
}
// Split and store borrows by their range starts.
let mut offset = 0;
let mut slices = HashMap::new();
for (&start, &end) in &splits {
// Split off leading bytes.
let mid = start
.checked_sub(offset)
.ok_or(ZipError::InvalidArchive("Overlapping ranges"))?;
if mid > bytes.len() {
return Err(ZipError::InvalidArchive("Offset exceeds EOF").into());
}
let (slice, remaining_bytes) = bytes.split_at_mut(mid);
offset += slice.len();
// Split off leading borrow of interest. Cannot underflow since `start < end`.
let mid = end - offset;
if mid > remaining_bytes.len() {
return Err(ZipError::InvalidArchive("Length exceeds EOF").into());
}
let (slice, remaining_bytes) = remaining_bytes.split_at_mut(mid);
offset += slice.len();
// Store borrow by its range start.
slices.insert(start, slice);
// Store remaining bytes.
bytes = remaining_bytes;
}
// Collect split borrows as file views.
for (&index, (data_range, crc32_range, central_crc32_range)) in &ranges {
let ambiguous_offset = || ZipError::InvalidArchive("Ambiguous offsets");
let file = NpyViewMut {
data: slices
.remove(&data_range.start)
.ok_or_else(ambiguous_offset)?,
crc32: slices
.remove(&crc32_range.start)
.map(as_array_mut)
.ok_or_else(ambiguous_offset)?,
central_crc32: slices
.remove(¢ral_crc32_range.start)
.map(as_array_mut)
.ok_or_else(ambiguous_offset)?,
status: ChecksumStatus::default(),
};
archive.files.insert(index, file);
}
Ok(archive)
}
/// Returns `true` iff the `.npz` file doesn't contain any viewable arrays.
///
/// Viewable arrays are neither directories, nor compressed, nor encrypted.
#[must_use]
pub fn is_empty(&self) -> bool {
self.names.is_empty()
}
/// Returns the number of viewable arrays in the `.npz` file.
///
/// Viewable arrays are neither directories, nor compressed, nor encrypted.
#[must_use]
pub fn len(&self) -> usize {
self.names.len()
}
/// Returns the names of all of the viewable arrays in the `.npz` file.
///
/// Viewable arrays are neither directories, nor compressed, nor encrypted.
pub fn names(&self) -> impl Iterator<Item = &str> {
self.names.keys().map(String::as_str)
}
/// Returns the names of all of the directories in the `.npz` file.
pub fn directory_names(&self) -> impl Iterator<Item = &str> {
self.directory_names.iter().map(String::as_str)
}
/// Returns the names of all of the compressed files in the `.npz` file.
pub fn compressed_names(&self) -> impl Iterator<Item = &str> {
self.compressed_names.iter().map(String::as_str)
}
/// Returns the names of all of the encrypted files in the `.npz` file.
pub fn encrypted_names(&self) -> impl Iterator<Item = &str> {
self.encrypted_names.iter().map(String::as_str)
}
/// Moves a mutable `.npy` file view by name out of the `.npz` file view.
///
/// # Errors
///
/// Viewing an `.npy` file can fail with [`ViewNpyError`]. Trying to view a directory,
/// compressed file, or encrypted file, fails with [`ViewNpzError::Directory`],
/// [`ViewNpzError::CompressedFile`], or [`ViewNpzError::CompressedFile`]. Fails with
/// [`ZipError::FileNotFound`] if the `name` is not found.
pub fn by_name(&mut self, name: &str) -> Result<NpyViewMut<'a>, ViewNpzError> {
self.by_index(self.names.get(name).copied().ok_or_else(|| {
if self.directory_names.contains(name) {
ViewNpzError::Directory
} else if self.compressed_names.contains(name) {
ViewNpzError::CompressedFile
} else if self.encrypted_names.contains(name) {
ViewNpzError::EncryptedFile
} else {
ZipError::FileNotFound.into()
}
})?)
}
/// Moves a mutable `.npy` file view by index in `0..len()` out of the `.npz` file view.
///
/// The index **does not** necessarily correspond to the index of the zip archive as
/// directories, compressed files, and encrypted files are skipped.
///
/// # Errors
///
/// Viewing an `.npy` file can fail with [`ViewNpyError`]. Fails with [`ZipError::FileNotFound`]
/// if the `index` is not found. Fails with [`ViewNpzError::MovedNpyViewMut`] if the mutable
/// `.npy` file view has already been moved out of the `.npz` file view.
pub fn by_index(&mut self, index: usize) -> Result<NpyViewMut<'a>, ViewNpzError> {
if index > self.names.len() {
Err(ZipError::FileNotFound.into())
} else {
self.files
.remove(&index)
.ok_or(ViewNpzError::MovedNpyViewMut)
}
}
}
/// Mutable view of memory-mapped `.npy` files within an `.npz` file.
///
/// Does **not** automatically [verify](`Self::verify`) the CRC-32 checksum but **does**
/// [update](`Self::update`) it on [`Drop::drop`] if [`view_mut`](`Self::view_mut`) has been invoked
/// and the checksum has not manually been updated by invoking [`update`](`Self::update`).
#[derive(Debug)]
pub struct NpyViewMut<'a> {
data: &'a mut [u8],
crc32: &'a mut [u8; 4],
central_crc32: &'a mut [u8; 4],
status: ChecksumStatus,
}
impl<'a> NpyViewMut<'a> {
/// CRC-32 checksum status.
#[must_use]
pub fn status(&self) -> ChecksumStatus {
self.status
}
/// Verifies and returns CRC-32 checksum by reading the whole array.
///
/// Changes checksum [`status`](`Self::status()`) to [`Outdated`](`ChecksumStatus::Outdated`)
/// if invalid or to [`Correct`](`ChecksumStatus::Correct`) if valid.
///
/// # Errors
///
/// Fails with [`ZipError::Io`] if the checksum is invalid.
pub fn verify(&mut self) -> Result<u32, ViewNpzError> {
self.status = ChecksumStatus::Outdated;
// Like the `zip` crate, verify only against central CRC-32.
let crc32 = crc32_verify(self.data, *self.central_crc32)?;
self.status = ChecksumStatus::Correct;
Ok(crc32)
}
/// Updates and returns CRC-32 checksum by reading the whole array.
///
/// Changes checksum [`status`](`Self::status()`) to [`Correct`](`ChecksumStatus::Correct`).
///
/// Automatically updated on [`Drop::drop`] iff checksum [`status`](`Self::status()`) is
/// [`Outdated`](`ChecksumStatus::Outdated`).
pub fn update(&mut self) -> u32 {
self.status = ChecksumStatus::Correct;
let crc32 = crc32_update(self.data);
*self.central_crc32 = crc32.to_le_bytes();
*self.crc32 = *self.central_crc32;
crc32
}
/// Returns an immutable view of a memory-mapped `.npy` file.
///
/// Iterates over `bool` array to ensure `0x00`/`0x01` values.
///
/// # Errors
///
/// Viewing an `.npy` file can fail with [`ViewNpyError`].
pub fn view<A, D>(&self) -> Result<ArrayView<A, D>, ViewNpzError>
where
A: ViewElement,
D: Dimension,
{
Ok(ArrayView::<A, D>::view_npy(self.data)?)
}
/// Returns a mutable view of a memory-mapped `.npy` file.
///
/// Iterates over `bool` array to ensure `0x00`/`0x01` values.
///
/// Changes checksum [`status`](`Self::status()`) to [`Outdated`](`ChecksumStatus::Outdated`).
///
/// # Errors
///
/// Viewing an `.npy` file can fail with [`ViewNpyError`].
pub fn view_mut<A, D>(&mut self) -> Result<ArrayViewMut<A, D>, ViewNpzError>
where
A: ViewMutElement,
D: Dimension,
{
self.status = ChecksumStatus::Outdated;
Ok(ArrayViewMut::<A, D>::view_mut_npy(self.data)?)
}
}
impl<'a> Drop for NpyViewMut<'a> {
fn drop(&mut self) {
if self.status == ChecksumStatus::Outdated {
self.update();
}
}
}
/// Checksum status of an [`NpyView`] or [`NpyViewMut`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChecksumStatus {
/// The checksum has not been computed and the data has not changed.
Unverified,
/// The checksum is correct and the data has not changed.
Correct,
/// The data may have changed.
Outdated,
}
impl Default for ChecksumStatus {
fn default() -> Self {
Self::Unverified
}
}
fn crc32_verify(bytes: &[u8], crc32: [u8; 4]) -> Result<u32, ZipError> {
let crc32 = u32::from_le_bytes(crc32);
if crc32_update(bytes) == crc32 {
Ok(crc32)
} else {
Err(ZipError::Io(io::Error::new(
io::ErrorKind::Other,
"Invalid checksum",
)))
}
}
#[must_use]
fn crc32_update(bytes: &[u8]) -> u32 {
let mut hasher = crc32fast::Hasher::new();
hasher.update(bytes);
hasher.finalize()
}
fn range_at<T>(index: T, range: Range<T>) -> Result<Range<usize>, ZipError>
where
T: TryInto<usize> + Copy,
{
index
.try_into()
.ok()
.and_then(|index| {
let start = range.start.try_into().ok()?.checked_add(index)?;
let end = range.end.try_into().ok()?.checked_add(index)?;
Some(start..end)
})
.ok_or(ZipError::InvalidArchive("Range overflow"))
}
fn slice_at<T>(bytes: &[u8], index: T, range: Range<T>) -> Result<&[u8], ZipError>
where
T: TryInto<usize> + Copy,
{
let range = range_at(index, range)?;
bytes
.get(range)
.ok_or(ZipError::InvalidArchive("Range exceeds EOF"))
}
#[must_use]
fn u16_at(bytes: &[u8], range: Range<usize>) -> u16 {
u16::from_le_bytes(bytes.get(range).unwrap().try_into().unwrap())
}
#[must_use]
fn u32_at(bytes: &[u8], range: Range<usize>) -> u32 {
u32::from_le_bytes(bytes.get(range).unwrap().try_into().unwrap())
}
#[must_use]
fn as_array_ref(slice: &[u8]) -> &[u8; 4] {
slice.try_into().unwrap()
}
#[must_use]
fn as_array_mut(slice: &mut [u8]) -> &mut [u8; 4] {
slice.try_into().unwrap()
}