SNAP Library 2.4, User Reference  2015-05-11 19:40:56
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TZipOut Class Reference

#include <zipfl.h>

Inherits TSOut.

Public Member Functions

 TZipOut (const TStr &_FNm)
 
 ~TZipOut ()
 
int PutCh (const char &Ch)
 
int PutBf (const void *LBf, const TSize &LBfL)
 
void Flush ()
 
- Public Member Functions inherited from TSOut
 TSOut ()
 
 TSOut (const TStr &Str)
 
virtual ~TSOut ()
 
void EnableLnTrunc (const int &_MxLnLen)
 
void DisableLnTrunc ()
 
virtual TFileId GetFileId () const
 
int PutMem (const TMem &Mem)
 
int PutCh (const char &Ch, const int &Chs)
 
int PutBool (const bool &Bool)
 
int PutInt (const int &Int)
 
int PutInt (const int &Int, const char *FmtStr)
 
int PutUInt (const uint &Int)
 
int PutUInt (const uint &Int, const char *FmtStr)
 
int PutFlt (const double &Flt)
 
int PutFlt (const double &Flt, const char *FmtStr)
 
int PutStr (const char *CStr)
 
int PutStr (const TChA &ChA)
 
int PutStr (const TStr &Str, const char *FmtStr)
 
int PutStr (const TStr &Str, const bool &ForceInLn=false)
 
int PutStrLn (const TStr &Str, const bool &ForceInLn=false)
 
int PutStrFmt (const char *FmtStr,...)
 
int PutStrFmtLn (const char *FmtStr,...)
 
int PutIndent (const int &IndentLev=1)
 
int PutLn (const int &Lns=1)
 
int PutDosLn (const int &Lns=1)
 
int PutSep (const int &NextStrLen=0)
 
int PutSepLn (const int &Lns=0)
 
void SaveCs ()
 
void SaveBf (const void *Bf, const TSize &BfL)
 
void Save (const bool &Bool)
 
void Save (const char &Ch)
 
void Save (const uchar &UCh)
 
void Save (const short &Short)
 
void Save (const ushort &UShort)
 
void Save (const int &Int)
 
void Save (const uint &UInt)
 
void Save (const int64 &Int)
 
void Save (const uint64 &UInt)
 
void Save (const double &Flt)
 
void Save (const sdouble &SFlt)
 
void Save (const ldouble &LFlt)
 
void Save (const char *CStr, const TSize &CStrLen)
 
void Save (const char *CStr)
 
void Save (TSIn &SIn, const TSize &BfL=-1)
 
void Save (const PSIn &SIn, const TSize &BfL=-1)
 
void Save (const void *Bf, const TSize &BfL)
 
TSOutoperator<< (const bool &Bool)
 
TSOutoperator<< (const uchar &UCh)
 
TSOutoperator<< (const char &Ch)
 
TSOutoperator<< (const short &Sh)
 
TSOutoperator<< (const ushort &USh)
 
TSOutoperator<< (const int &Int)
 
TSOutoperator<< (const uint &Int)
 
TSOutoperator<< (const int64 &Int)
 
TSOutoperator<< (const uint64 &UInt)
 
TSOutoperator<< (const float &Flt)
 
TSOutoperator<< (const double &Double)
 
TSOutoperator<< (const long double &LDouble)
 
TSOutoperator<< (const TSOutMnp &Mnp)
 
TSOutoperator<< (TSOut &(*FuncPt)(TSOut &))
 
TSOutoperator<< (TSIn &SIn)
 
TSOutoperator<< (PSIn &SIn)
 
- Public Member Functions inherited from TSBase
 TSBase (const TSStr &Nm)
 
virtual ~TSBase ()
 
virtual TStr GetSNm () const
 

Static Public Member Functions

static PSOut New (const TStr &FNm)
 
static bool IsZipFNm (const TStr &FNm)
 Check whether the file extension of FNm is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2). More...
 
static bool IsZipExt (const TStr &FNmExt)
 Check whether the file extension FNmExt is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2). More...
 
static TStr GetCmd (const TStr &ZipFNm)
 Return a command-line string that is executed in order to decompress a file to standard output. More...
 
static PSOut NewIfZip (const TStr &FNm)
 

Private Member Functions

void FlushBf ()
 
void CreateZipProcess (const TStr &Cmd, const TStr &ZipFNm)
 
 TZipOut ()
 
 TZipOut (const TZipOut &)
 
TZipOutoperator= (const TZipOut &)
 

Static Private Member Functions

static void FillFExtToCmdH ()
 

Private Attributes

FILE * ZipStdinRd
 
FILE * ZipStdinWr
 
char * Bf
 
TSize BfL
 

Static Private Attributes

static const TSize MxBfL =4*1024
 
static TStrStrH FExtToCmdH
 

Additional Inherited Members

- Static Public Attributes inherited from TSOut
static const TPt< TSOutStdOut =PSOut(new TStdOut())
 
- Protected Attributes inherited from TSBase
TCRef CRef
 
TSStr SNm
 
TCs Cs
 

Detailed Description

Compressed File Output Stream. The class directly writes to a compressed file. This is eachieved by TZipFl outputing into a pipe from which 7ZIP then reads and compresses. The class requires 7ZIP to be installed on the machine. Go to http://www.7-zip.org to install the software. 7z (7z.exe) is an executable and can decompress the following formats: .gz, .7z, .rar, .zip, .cab, .arj. bzip2. The class TZIpOut expects that '7z' ('7z.exe') is in the working path. Note2: For 7z to work properly you need both the 7z executable and the directory 'Codecs'. Note3: Use TZipIn::SevenZipPath to set the path to 7z executable.

Definition at line 74 of file zipfl.h.

Constructor & Destructor Documentation

TZipOut::TZipOut ( )
private
TZipOut::TZipOut ( const TZipOut )
private
TZipOut::TZipOut ( const TStr _FNm)

Definition at line 345 of file zipfl.cpp.

345  : TSBase(FNm.CStr()), TSOut(FNm), ZipStdinRd(NULL), ZipStdinWr(NULL), Bf(NULL), BfL(0){
346  EAssertR(! FNm.Empty(), "Empty file-name.");
347  #ifdef GLib_WIN
348  // create pipes
349  SECURITY_ATTRIBUTES saAttr;
350  saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
351  saAttr.bInheritHandle = TRUE;
352  saAttr.lpSecurityDescriptor = NULL;
353  // Create a pipe for the child process's STDOUT.
354  EAssertR(CreatePipe(&ZipStdinRd, &ZipStdinWr, &saAttr, 0), "Stdout pipe creation failed");
355  // Ensure the read handle to the pipe for STDOUT is not inherited.
356  SetHandleInformation(ZipStdinWr, HANDLE_FLAG_INHERIT, 0);
357  #else
358  // no implementation necessary
359  #endif
360  CreateZipProcess(GetCmd(FNm), FNm);
361  Bf=new char[MxBfL]; BfL=0;
362 }
static TStr GetCmd(const TStr &ZipFNm)
Return a command-line string that is executed in order to decompress a file to standard output...
Definition: zipfl.cpp:429
TSBase(const TSStr &Nm)
Definition: fl.h:50
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
static const TSize MxBfL
Definition: zipfl.h:76
FILE * ZipStdinRd
Definition: zipfl.h:81
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
TSOut()
Definition: fl.h:136
FILE * ZipStdinWr
Definition: zipfl.h:81
void CreateZipProcess(const TStr &Cmd, const TStr &ZipFNm)
Definition: zipfl.cpp:312
TZipOut::~TZipOut ( )

Definition at line 368 of file zipfl.cpp.

368  {
369  if (BfL!=0) { FlushBf(); }
370  #ifdef GLib_WIN
371  if (ZipStdinWr != NULL) { EAssertR(CloseHandle(ZipStdinWr), "Closing write-end of pipe failed"); }
372  if (ZipStdinRd != NULL) { EAssertR(CloseHandle(ZipStdinRd), "Closing read-end of pipe failed"); }
373  #else
374  if (ZipStdinWr != NULL) { EAssertR(pclose(ZipStdinWr) != -1, "Closing of the process failed"); }
375  #endif
376  if (Bf!=NULL) { delete[] Bf; }
377 }
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
FILE * ZipStdinRd
Definition: zipfl.h:81
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
FILE * ZipStdinWr
Definition: zipfl.h:81
void FlushBf()
Definition: zipfl.cpp:301

Member Function Documentation

void TZipOut::CreateZipProcess ( const TStr Cmd,
const TStr ZipFNm 
)
private

Definition at line 312 of file zipfl.cpp.

312  {
313  const TStr CmdLine = TStr::Fmt("%s %s", Cmd.CStr(), ZipFNm.CStr());
314  #ifdef GLib_WIN
315  PROCESS_INFORMATION piProcInfo;
316  STARTUPINFO siStartInfo;
317  ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION));
318  ZeroMemory( &siStartInfo, sizeof(STARTUPINFO));
319  siStartInfo.cb = sizeof(STARTUPINFO);
320  siStartInfo.hStdInput = ZipStdinRd;
321  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
322  // Create the child process.
323  const BOOL FuncRetn = CreateProcess(NULL,
324  (LPSTR) CmdLine.CStr(), // command line
325  NULL, // process security attributes
326  NULL, // primary thread security attributes
327  TRUE, // handles are inherited
328  0, // creation flags
329  NULL, // use parent's environment
330  NULL, // use parent's current directory
331  &siStartInfo, // STARTUPINFO pointer
332  &piProcInfo); // receives PROCESS_INFORMATION
333  EAssertR(FuncRetn!=0, TStr::Fmt("Can not execute '%s'", CmdLine.CStr()).CStr());
334  CloseHandle(piProcInfo.hProcess);
335  CloseHandle(piProcInfo.hThread);
336  #else
337  ZipStdinWr = popen(CmdLine.CStr(),"w");
338  if (ZipStdinWr == NULL) { // try using SevenZipPath
339  ZipStdinWr = popen((TZipIn::SevenZipPath+"/"+CmdLine).CStr(), "r");
340  }
341  EAssertR(ZipStdinWr != NULL, TStr::Fmt("Can not execute '%s'", CmdLine.CStr()).CStr());
342  #endif
343 }
static TStr SevenZipPath
Definition: zipfl.h:17
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
FILE * ZipStdinRd
Definition: zipfl.h:81
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
FILE * ZipStdinWr
Definition: zipfl.h:81
char * CStr()
Definition: dt.h:476
void TZipOut::FillFExtToCmdH ( )
staticprivate

Definition at line 410 of file zipfl.cpp.

410  {
411  // 7za compress: "a -y -bd -si{CompressedFNm}"
412  #ifdef GLib_WIN
413  const char* ZipCmd = "7z.exe a -y -bd -si";
414  #else
415  const char* ZipCmd = "7za a -y -bd -si";
416  #endif
417  if (FExtToCmdH.Empty()) {
418  FExtToCmdH.AddDat(".gz", ZipCmd);
419  FExtToCmdH.AddDat(".7z", ZipCmd);
420  FExtToCmdH.AddDat(".rar", ZipCmd);
421  FExtToCmdH.AddDat(".zip", ZipCmd);
422  FExtToCmdH.AddDat(".cab", ZipCmd);
423  FExtToCmdH.AddDat(".arj", ZipCmd);
424  FExtToCmdH.AddDat(".bzip2", ZipCmd);
425  FExtToCmdH.AddDat(".bz2", ZipCmd);
426  }
427 }
bool Empty() const
Definition: hash.h:185
static TStrStrH FExtToCmdH
Definition: zipfl.h:77
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void TZipOut::Flush ( )
virtual

Implements TSOut.

Definition at line 396 of file zipfl.cpp.

396  {
397  FlushBf();
398  #ifdef GLib_WIN
399  EAssertR(FlushFileBuffers(ZipStdinWr)!=0, "Can not flush file '"+GetSNm()+"'.");
400  #else
401  EAssertR(fflush(ZipStdinWr)==0, "Can not flush file '"+GetSNm()+"'.");
402  #endif
403 }
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
virtual TStr GetSNm() const
Definition: fl.cpp:20
FILE * ZipStdinWr
Definition: zipfl.h:81
void FlushBf()
Definition: zipfl.cpp:301
void TZipOut::FlushBf ( )
private

Definition at line 301 of file zipfl.cpp.

301  {
302  #ifdef GLib_WIN
303  DWORD BytesOut;
304  EAssertR(WriteFile(ZipStdinWr, Bf, DWORD(BfL), &BytesOut, NULL)!=0, "Error writting to the file '"+GetSNm()+"'.");
305  #else
306  size_t BytesOut = fwrite(Bf, 1, BfL, ZipStdinWr);
307  #endif
308  EAssert(BytesOut == BfL);
309  BfL = 0;
310 }
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
#define EAssert(Cond)
Definition: bd.h:280
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
virtual TStr GetSNm() const
Definition: fl.cpp:20
FILE * ZipStdinWr
Definition: zipfl.h:81
TStr TZipOut::GetCmd ( const TStr ZipFNm)
static

Return a command-line string that is executed in order to decompress a file to standard output.

Definition at line 429 of file zipfl.cpp.

429  {
431  const TStr Ext = ZipFNm.GetFExt().GetLc();
432  EAssertR(FExtToCmdH.IsKey(Ext), TStr::Fmt("Unsupported file extension '%s'", Ext.CStr()));
433  return FExtToCmdH.GetDat(Ext)+ZipFNm.GetFMid();
434 }
TStr GetFMid() const
Definition: dt.cpp:1403
bool Empty() const
Definition: hash.h:185
TStr GetFExt() const
Definition: dt.cpp:1421
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
TStr GetLc() const
Definition: dt.h:499
static TStrStrH FExtToCmdH
Definition: zipfl.h:77
static void FillFExtToCmdH()
Definition: zipfl.cpp:410
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
char * CStr()
Definition: dt.h:476
bool IsKey(const TKey &Key) const
Definition: hash.h:216
bool TZipOut::IsZipExt ( const TStr FNmExt)
static

Check whether the file extension FNmExt is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).

Definition at line 405 of file zipfl.cpp.

405  {
407  return FExtToCmdH.IsKey(FNmExt);
408 }
bool Empty() const
Definition: hash.h:185
static TStrStrH FExtToCmdH
Definition: zipfl.h:77
static void FillFExtToCmdH()
Definition: zipfl.cpp:410
bool IsKey(const TKey &Key) const
Definition: hash.h:216
static bool TZipOut::IsZipFNm ( const TStr FNm)
inlinestatic

Check whether the file extension of FNm is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).

Definition at line 103 of file zipfl.h.

103 { return IsZipExt(FNm.GetFExt()); }
TStr GetFExt() const
Definition: dt.cpp:1421
static bool IsZipExt(const TStr &FNmExt)
Check whether the file extension FNmExt is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).
Definition: zipfl.cpp:405
PSOut TZipOut::New ( const TStr FNm)
static

Definition at line 364 of file zipfl.cpp.

364  {
365  return PSOut(new TZipOut(FNm));
366 }
TPt< TSOut > PSOut
Definition: fl.h:211
static PSOut TZipOut::NewIfZip ( const TStr FNm)
inlinestatic

Definition at line 108 of file zipfl.h.

108 { return IsZipFNm(FNm) ? New(FNm) : TFOut::New(FNm); }
Definition: fl.h:319
static PSOut New(const TStr &FNm)
Definition: zipfl.cpp:364
static bool IsZipFNm(const TStr &FNm)
Check whether the file extension of FNm is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).
Definition: zipfl.h:103
TZipOut& TZipOut::operator= ( const TZipOut )
private
int TZipOut::PutBf ( const void *  LBf,
const TSize LBfL 
)
virtual

Implements TSOut.

Definition at line 384 of file zipfl.cpp.

384  {
385  int LBfS=0;
386  if (BfL+LBfL>MxBfL){
387  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
388  LBfS+=PutCh(((char*)LBf)[LBfC]);}
389  } else {
390  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
391  LBfS+=(Bf[BfL++]=((char*)LBf)[LBfC]);}
392  }
393  return LBfS;
394 }
int PutCh(const char &Ch)
Definition: zipfl.cpp:379
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
size_t TSize
Definition: bd.h:58
static const TSize MxBfL
Definition: zipfl.h:76
int TZipOut::PutCh ( const char &  Ch)
virtual

Implements TSOut.

Definition at line 379 of file zipfl.cpp.

379  {
380  if (BfL==MxBfL) {FlushBf();}
381  return Bf[BfL++]=Ch;
382 }
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
static const TSize MxBfL
Definition: zipfl.h:76
void FlushBf()
Definition: zipfl.cpp:301

Member Data Documentation

char* TZipOut::Bf
private

Definition at line 83 of file zipfl.h.

TSize TZipOut::BfL
private

Definition at line 84 of file zipfl.h.

TStrStrH TZipOut::FExtToCmdH
staticprivate

Definition at line 77 of file zipfl.h.

const TSize TZipOut::MxBfL =4*1024
staticprivate

Definition at line 76 of file zipfl.h.

FILE* TZipOut::ZipStdinRd
private

Definition at line 81 of file zipfl.h.

FILE * TZipOut::ZipStdinWr
private

Definition at line 81 of file zipfl.h.


The documentation for this class was generated from the following files: