Skip to content
Snippets Groups Projects
Commit bdfc8712 authored by Thomas Boy's avatar Thomas Boy
Browse files

found a way to read the file from the lib archive to the memory without writing a file

parent a1152c28
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,7 @@ based on original demo.cpp ...@@ -15,6 +15,7 @@ based on original demo.cpp
//cpp c //cpp c
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <vector>
#include <cstdlib> #include <cstdlib>
#include <stdio.h> /* printf, scanf, puts, NULL */ #include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */ #include <stdlib.h> /* srand, rand */
...@@ -51,6 +52,15 @@ bool cmdOptionExists(char** begin, char** end, const std::string& option) ...@@ -51,6 +52,15 @@ bool cmdOptionExists(char** begin, char** end, const std::string& option)
} }
static std::vector<char> copy_data(struct archive *aw, unsigned char *buffer);
static void errmsg(const char *);
static void fail(const char *, const char *, int);
static void msg(const char *);
static void warn(const char *, const char *);
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
std::cout << "using produce bk " <<PROCESS_CENTER_VERSION_MAJOR <<"."<< PROCESS_CENTER_VERSION_MINOR << endl; std::cout << "using produce bk " <<PROCESS_CENTER_VERSION_MAJOR <<"."<< PROCESS_CENTER_VERSION_MINOR << endl;
...@@ -105,14 +115,179 @@ int main(int argc, char * argv[]) ...@@ -105,14 +115,179 @@ int main(int argc, char * argv[])
struct archive_entry *entry; struct archive_entry *entry;
int r; int r;
int flags = ARCHIVE_EXTRACT_TIME; // see https://linux.die.net/man/3/archive_write_disk for more flags int flags = ARCHIVE_EXTRACT_TIME; // see https://linux.die.net/man/3/archive_write_disk for more flags
const char *filename = "recTest.tar"; const char *filename = "data_sized.tar";
a = archive_read_new(); a = archive_read_new();
ext = archive_write_disk_new(); ext = archive_write_disk_new();
archive_write_disk_set_options(ext, flags); archive_write_disk_set_options(ext, flags);
/*
* Note: archive_write_disk_set_standard_lookup() is useful
* here, but it requires library routines that can add 500k or
* more to a static executable.
*/
archive_read_support_format_tar(a);
/*
* On my system, enabling other archive formats adds 20k-30k
* each. Enabling gzip decompression adds about 20k.
* Enabling bzip2 is more expensive because the libbz2 library
* isn't very well factored.
*/
if (filename != NULL && strcmp(filename, "-") == 0)
filename = NULL;
if ((r = archive_read_open_filename(a, filename, 10240)))
fail("archive_read_open_filename()",
archive_error_string(a), r);
int counter = 0;
for (;;) {
r = archive_read_next_header(a, &entry);
if (r == ARCHIVE_EOF)
break;
if (r != ARCHIVE_OK)
fail("archive_read_next_header()",
archive_error_string(a), 1);
msg("x ");
///char *buffer = malloc (sizeof(char) * 400000);
Mat img =Mat::zeros(1936,1456,CV_8UC3);
//new way
//std::ifstream file("img.jpg");
std::vector<char> vec;
// file >> std::noskipws;
// std::copy(std::istream_iterator<char>(file), std::istream_iterator<char>(), std::back_inserter(data));
msg(archive_entry_pathname(entry));
{
r = archive_write_header(ext, entry);
if (r != ARCHIVE_OK)
warn("archive_write_header()",
archive_error_string(ext));
else
{
vec = copy_data(a, img.data);//no // we cast the data pointer to void, because we know what we do ??
r = archive_write_finish_entry(ext);
if (r != ARCHIVE_OK)
fail("archive_write_finish_entry()",
archive_error_string(ext), 1);
}
}
msg("\n");
if(counter > 10) exit(1);
counter ++;
// capture.release(); Mat img2 = imdecode(Mat(vec), 1);
cvDestroyAllWindows();
//we show what we got
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", img2 ); // Show our image inside it.
cv::imwrite("foo.jpg", img2);
waitKey(0);
}
archive_read_close(a);
archive_read_free(a);
archive_write_close(ext);
archive_write_free(ext);
return 0; return 0;
} }
static std::vector<char> copy_data(struct archive *aw, unsigned char *buffer)
{
int r;
const void *buff;
std::vector<char> vec;
size_t size;
#if ARCHIVE_VERSION_NUMBER >= 3000000
int64_t offset;
#else
off_t offset;
#endif
unsigned int myOffsetCounter = 0;
int counterIteration = 0;
ofstream myfile;
myfile.open ("tmpPicture.jpg", ios::out | ios::binary);
if (!myfile.is_open())
return vec;
for (;;) {
r = archive_read_data_block(aw, &buff, &size, &offset);
if (r == ARCHIVE_EOF)
{
return vec; // everything fine
}
if (r != ARCHIVE_OK)
{
cerr << "error during read archive "<<endl;
return vec;
}
//r = archive_write_data_block(aw, buff, size, offset);
//we mem copy the buffer
cout <<counterIteration++<< " offset: "<< offset<< " size: "<< size<<endl;
//memcpy( &buffer[myOffsetCounter], (char*) buff, size * sizeof( char ) );
//we simply copy it to the end
//std::copy ( buff, buff+size, vec.end() );
myOffsetCounter += size;
//a good discussion : https://stackoverflow.com/questions/259297/how-do-you-copy-the-contents-of-an-array-to-a-stdvector-in-c-without-looping
// // Method 2: Same as 1 but pre-extend the vector by the size of the array using reserve
// {
// vec.reserve(vec.size() + size);
// std::copy((char*) &buff[0],(char*) &buff[size], back_inserter(vec));
// }
// Method 4: vector::insert
{
//we rename our pointer
char *foo = (char*) buff;
vec.insert(vec.end(), &foo[0], &foo[size]);
}
myfile.write ((char*)buff,size);
// if (r != ARCHIVE_OK) {
// warn("archive_write_data_block()",
// archive_error_string(aw));
// return (r);
// }
}
myfile.close();
return vec;
}
static void
msg(const char *m)
{
write(1, m, strlen(m));
}
static void
errmsg(const char *m)
{
write(2, m, strlen(m));
}
static void
warn(const char *f, const char *m)
{
errmsg(f);
errmsg(" failed: ");
errmsg(m);
errmsg("\n");
}
static void
fail(const char *f, const char *m, int r)
{
warn(f, m);
exit(r);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment