- Factor out buffer size constant for files;

- Avoid copy paste;
- Minor code style fixes;
This commit is contained in:
vng 2013-03-04 21:03:47 +03:00 committed by Alex Zolotarev
parent 581c0df1b6
commit 987f5932ae
7 changed files with 86 additions and 89 deletions

View file

@ -93,3 +93,4 @@ HEADERS += \
uri.hpp \
zip_creator.hpp \
file_name_utils.hpp \
constants.hpp \

View file

@ -1,69 +1,54 @@
#include "../../testing/testing.hpp"
#include "../../map/framework.hpp"
#include "../../coding/zip_creator.hpp"
#include "../../coding/zip_reader.hpp"
#include "../../coding/internal/file_data.hpp"
#include "../../coding/writer.hpp"
#include "../../base/scope_guard.hpp"
#include "../../platform/platform.hpp"
#include "../../coding/file_writer.hpp"
#include "../../coding/constants.hpp"
#include "../../std/string.hpp"
#include "../../std/vector.hpp"
#include "../../std/iostream.hpp"
UNIT_TEST(Create_Zip_From_Big_File)
namespace
{
string const name = "testfileforzip.txt";
void CreateAndTestZip(string const & filePath, string const & zipPath)
{
my::FileData f(name, my::FileData::OP_WRITE_TRUNCATE);
string z(1024*512 + 1, '1');
f.Write(z.c_str(), z.size());
TEST(CreateZipFromPathDeflatedAndDefaultCompression(filePath, zipPath), ());
vector<string> files;
ZipFileReader::FilesList(zipPath, files);
string const unzippedFile = "unzipped.tmp";
ZipFileReader::UnzipFile(zipPath, files[0], unzippedFile);
TEST(my::IsEqualFiles(filePath, unzippedFile), ());
TEST(my::DeleteFileX(filePath), ());
TEST(my::DeleteFileX(zipPath), ());
TEST(my::DeleteFileX(unzippedFile), ());
}
string const zipName = "testzip.zip";
TEST(createZipFromPathDeflatedAndDefaultCompression(name, zipName), ());
vector<string> files;
ZipFileReader::FilesList(zipName, files);
string const unzippedFile = "unzipped.tmp";
ZipFileReader::UnzipFile(zipName, files[0], unzippedFile);
TEST(my::IsEqualFiles(name, unzippedFile),());
TEST(my::DeleteFileX(name), ());
TEST(my::DeleteFileX(zipName), ());
TEST(my::DeleteFileX(unzippedFile), ());
}
UNIT_TEST(Create_zip)
UNIT_TEST(CreateZip_BigFile)
{
string const name = "testfileforzip.txt";
{
my::FileData f(name, my::FileData::OP_WRITE_TRUNCATE);
FileWriter f(name);
string s(READ_FILE_BUFFER_SIZE + 1, '1');
f.Write(s.c_str(), s.size());
}
CreateAndTestZip(name, "testzip.zip");
}
UNIT_TEST(CreateZip_Smoke)
{
string const name = "testfileforzip.txt";
{
FileWriter f(name);
f.Write(name.c_str(), name.size());
}
string const zipName = "testzip.zip";
TEST(createZipFromPathDeflatedAndDefaultCompression(name, zipName), ());
vector<string> files;
ZipFileReader::FilesList(zipName, files);
string const unzippedFile = "unzipped.tmp";
ZipFileReader::UnzipFile(zipName, files[0], unzippedFile);
TEST(my::IsEqualFiles(name, unzippedFile),());
TEST(my::DeleteFileX(name), ());
TEST(my::DeleteFileX(zipName), ());
TEST(my::DeleteFileX(unzippedFile), ());
CreateAndTestZip(name, "testzip.zip");
}

3
coding/constants.hpp Normal file
View file

@ -0,0 +1,3 @@
#pragma once
static const size_t READ_FILE_BUFFER_SIZE = 512 * 1024;

View file

@ -2,6 +2,7 @@
#include "../reader.hpp" // For Reader exceptions.
#include "../writer.hpp" // For Writer exceptions.
#include "../constants.hpp"
#include "../../base/exception.hpp"
#include "../../base/logging.hpp"
@ -270,7 +271,7 @@ bool IsEqualFiles(string const & firstFile, string const & secondFile)
if (first.Size() != second.Size())
return false;
size_t const bufSize = 512 * 1024;
size_t const bufSize = READ_FILE_BUFFER_SIZE;
vector<char> buf1, buf2;
buf1.resize(bufSize);
buf2.resize(bufSize);
@ -279,15 +280,17 @@ bool IsEqualFiles(string const & firstFile, string const & secondFile)
while (currSize < fileSize)
{
size_t toRead = fileSize - currSize;
if (toRead > bufSize)
toRead = bufSize;
size_t const toRead = min(bufSize, fileSize - currSize);
first.Read(currSize, &buf1[0], toRead);
second.Read(currSize, &buf2[0], toRead);
if (buf1 != buf2)
return false;
currSize += toRead;
}
return true;
}

View file

@ -2,75 +2,80 @@
#include "../../coding/file_name_utils.hpp"
#include "../../coding/internal/file_data.hpp"
#include "../../coding/constants.hpp"
#include "../../std/vector.hpp"
#include "../../std/iostream.hpp"
#include "../../std/ctime.hpp"
#include "../../std/algorithm.hpp"
#include "../../3party/zlib/contrib/minizip/zip.h"
class ZipHandle
{
public:
zipFile m_zipFile;
ZipHandle(string const & filePath)
{
m_zipFile = zipOpen(filePath.c_str(), 0);
}
~ZipHandle()
{
if (m_zipFile)
zipClose(m_zipFile, NULL);
}
};
namespace
{
void CreateTMZip(tm_zip & res)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
res.tm_sec = timeinfo->tm_sec;
res.tm_min = timeinfo->tm_min;
res.tm_hour = timeinfo->tm_hour;
res.tm_mday = timeinfo->tm_mday;
res.tm_mon = timeinfo->tm_mon;
res.tm_year = timeinfo->tm_year;
}
struct ZipHandle
{
zipFile m_zipFile;
ZipHandle(string const & filePath)
{
m_zipFile = zipOpen(filePath.c_str(), 0);
}
~ZipHandle()
{
if (m_zipFile)
zipClose(m_zipFile, NULL);
}
};
void CreateTMZip(tm_zip & res)
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
res.tm_sec = timeinfo->tm_sec;
res.tm_min = timeinfo->tm_min;
res.tm_hour = timeinfo->tm_hour;
res.tm_mday = timeinfo->tm_mday;
res.tm_mon = timeinfo->tm_mon;
res.tm_year = timeinfo->tm_year;
}
}
bool createZipFromPathDeflatedAndDefaultCompression(string const & filePath, string const & zipFilePath)
bool CreateZipFromPathDeflatedAndDefaultCompression(string const & filePath, string const & zipFilePath)
{
ZipHandle zip(zipFilePath);
if (!zip.m_zipFile)
return false;
// Special syntax to initialize struct with zeroes
zip_fileinfo zipInfo = zip_fileinfo();
CreateTMZip(zipInfo.tmz_date);
string fileName = filePath;
my::GetNameFromFullPath(fileName);
if (zipOpenNewFileInZip (zip.m_zipFile, fileName.c_str(), &zipInfo, NULL, 0, NULL, 0, "ZIP from MapsWithMe", Z_DEFLATED, Z_DEFAULT_COMPRESSION) < 0)
if (zipOpenNewFileInZip(zip.m_zipFile, fileName.c_str(), &zipInfo,
NULL, 0, NULL, 0, "ZIP from MapsWithMe", Z_DEFLATED, Z_DEFAULT_COMPRESSION) < 0)
{
return false;
}
my::FileData f(filePath, my::FileData::OP_READ);
size_t const bufSize = 512 * 1024;
size_t const bufSize = READ_FILE_BUFFER_SIZE;
vector<char> buffer(bufSize);
size_t const fileSize = f.Size();
size_t currSize = 0;
while (currSize < fileSize)
{
size_t toRead = fileSize - currSize;
if (toRead > bufSize)
toRead = bufSize;
size_t const toRead = min(bufSize, fileSize - currSize);
f.Read(currSize, &buffer[0], toRead);
if (ZIP_OK != zipWriteInFileInZip (zip.m_zipFile, &buffer[0], toRead))
if (ZIP_OK != zipWriteInFileInZip(zip.m_zipFile, &buffer[0], toRead))
return false;
currSize += toRead;
}
return true;
}

View file

@ -1,4 +1,4 @@
#include "../../std/string.hpp"
bool createZipFromPathDeflatedAndDefaultCompression(string const & filePath, string const & zipFilePath);
bool CreateZipFromPathDeflatedAndDefaultCompression(string const & filePath, string const & zipFilePath);

View file

@ -240,7 +240,7 @@
[catName setString:@"MapsWithMe"];
NSMutableString * kmzFile = [NSMutableString stringWithString:filePath];
[kmzFile replaceCharactersInRange:NSMakeRange([filePath length] - 1, 1) withString:@"z"];
if (createZipFromPathDeflatedAndDefaultCompression([filePath UTF8String], [kmzFile UTF8String]))
if (CreateZipFromPathDeflatedAndDefaultCompression([filePath UTF8String], [kmzFile UTF8String]))
{
[self sendBookmarksWithExtension:@".kmz" andType:@"application/vnd.google-earth.kmz" andFile:kmzFile andCategory:catName];
}