Permalink
Browse files

Do extraction in parallel.

  • Loading branch information...
jpakkane committed Apr 23, 2016
1 parent c03255f commit 345e8a237d1c10ba6f1d382320c528715c633ca8
Showing with 22 additions and 7 deletions.
  1. +5 −3 meson.build
  2. +17 −4 zipfile.cpp
View
@@ -1,8 +1,10 @@
-project('jzip', 'cpp', default_options : ['cpp_std=c++14', 'warnlevel=3'])
+project('jzip', 'cpp',
+ default_options : ['cpp_std=c++14', 'warning_level=3'])
zdep = dependency('zlib')
+threaddep = dependency('threads')
executable('junzip',
'junzip.cpp', 'zipfile.cpp', 'zimp.cpp',
- dependencies : zdep)
-
+ dependencies : [zdep, threaddep]
+)
View
@@ -26,6 +26,8 @@
#include<cstring>
#include<stdexcept>
#include<memory>
+#include<future>
+#include<thread>
const constexpr int LOCAL_SIG = 0x04034b50;
@@ -94,14 +96,25 @@ void ZipFile::unzip() const {
throw std::runtime_error(msg);
}
unsigned char *file_start = (unsigned char*)(data.get());
+ std::vector<std::future<void>> futures;
for(size_t i=0; i<entries.size(); i++) {
if(entries[i].compression != 8) {
printf("Skipping %s, is not compressed with deflate.\n", entries[i].fname.c_str());
continue;
}
- printf("Uncompressing %s.\n", entries[i].fname.c_str());
- inflate_to_file(file_start + data_offsets[i],
- entries[i].compressed_size,
- entries[i].fname);
+// printf("Uncompressing %s.\n", entries[i].fname.c_str());
+ auto deftask = [this, file_start, i](){
+ inflate_to_file(file_start + data_offsets[i],
+ entries[i].compressed_size,
+ entries[i].fname);
+ };
+ futures.emplace_back(std::async(std::launch::async, deftask));
+ }
+ for(const auto &i : futures) {
+ try {
+ i.wait();
+ } catch(const std::exception &e) {
+ printf("%s\n", e.what());
+ }
}
}

0 comments on commit 345e8a2

Please sign in to comment.