SNAP Library 2.0, Developer Reference  2013-05-13 16:33:57
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TZipOut Class Reference

#include <zipfl.h>

Inheritance diagram for TZipOut:
Collaboration diagram for TZipOut:

List of all members.

Public Member Functions

 TZipOut (const TStr &_FNm)
 ~TZipOut ()
int PutCh (const char &Ch)
int PutBf (const void *LBf, const TSize &LBfL)
void Flush ()

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).
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).
static TStr GetCmd (const TStr &ZipFNm)
 Return a command-line string that is executed in order to decompress a file to standard output.
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

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'.

Definition at line 68 of file zipfl.h.


Constructor & Destructor Documentation

TZipOut::TZipOut ( ) [private]

Referenced by New().

Here is the caller graph for this function:

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

Definition at line 341 of file zipfl.cpp.

References Bf, BfL, CreateZipProcess(), EAssertR, TStr::Empty(), GetCmd(), MxBfL, ZipStdinRd, and ZipStdinWr.

                                : TSBase(FNm.CStr()), TSOut(FNm), ZipStdinRd(NULL), ZipStdinWr(NULL), Bf(NULL), BfL(0){
  EAssertR(! FNm.Empty(), "Empty file-name.");
  #ifdef GLib_WIN
  // create pipes
  SECURITY_ATTRIBUTES saAttr;
  saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
  saAttr.bInheritHandle = TRUE;
  saAttr.lpSecurityDescriptor = NULL;
  // Create a pipe for the child process's STDOUT.
  EAssertR(CreatePipe(&ZipStdinRd, &ZipStdinWr, &saAttr, 0), "Stdout pipe creation failed");
  // Ensure the read handle to the pipe for STDOUT is not inherited.
  SetHandleInformation(ZipStdinWr, HANDLE_FLAG_INHERIT, 0);
  #else
  // no implementation necessary
  #endif
  CreateZipProcess(GetCmd(FNm), FNm);
  Bf=new char[MxBfL];  BfL=0;
}

Here is the call graph for this function:

Definition at line 364 of file zipfl.cpp.

References Bf, BfL, EAssertR, FlushBf(), ZipStdinRd, and ZipStdinWr.

                  {
  if (BfL!=0) { FlushBf(); }
  #ifdef GLib_WIN
  if (ZipStdinWr != NULL) { EAssertR(CloseHandle(ZipStdinWr), "Closing write-end of pipe failed"); }
  if (ZipStdinRd != NULL) { EAssertR(CloseHandle(ZipStdinRd), "Closing read-end of pipe failed"); }
  #else
  if (ZipStdinWr != NULL) { EAssertR(pclose(ZipStdinWr) != -1, "Closing of the process failed"); }
  #endif
  if (Bf!=NULL) { delete[] Bf; }
}

Here is the call graph for this function:


Member Function Documentation

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

Definition at line 311 of file zipfl.cpp.

References TStr::CStr(), EAssertR, TStr::Fmt(), ZipStdinRd, and ZipStdinWr.

Referenced by TZipOut().

                                                                  {
  const TStr CmdLine = TStr::Fmt("%s %s", Cmd.CStr(), ZipFNm.CStr());
  #ifdef GLib_WIN
  PROCESS_INFORMATION piProcInfo;
  STARTUPINFO siStartInfo;
  ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION));
  ZeroMemory( &siStartInfo, sizeof(STARTUPINFO));
  siStartInfo.cb = sizeof(STARTUPINFO);
  siStartInfo.hStdInput = ZipStdinRd;
  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  // Create the child process.
  const BOOL FuncRetn = CreateProcess(NULL,
    (LPSTR) CmdLine.CStr(),  // command line
    NULL,          // process security attributes
    NULL,          // primary thread security attributes
    TRUE,          // handles are inherited
    0,             // creation flags
    NULL,          // use parent's environment
    NULL,          // use parent's current directory
    &siStartInfo,  // STARTUPINFO pointer
    &piProcInfo);  // receives PROCESS_INFORMATION
  EAssertR(FuncRetn!=0, TStr::Fmt("Can not execute '%s'", CmdLine.CStr()).CStr());
  CloseHandle(piProcInfo.hProcess);
  CloseHandle(piProcInfo.hThread);
  #else
  ZipStdinWr = popen(CmdLine.CStr(),"w");
  EAssertR(ZipStdinWr != NULL,  TStr::Fmt("Can not execute '%s'", CmdLine.CStr()).CStr());
  #endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void TZipOut::FillFExtToCmdH ( ) [static, private]

Definition at line 406 of file zipfl.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Empty(), and FExtToCmdH.

Referenced by GetCmd(), and IsZipExt().

                             {
   // 7za compress: "a -y -bd -si{CompressedFNm}"
  #ifdef GLib_WIN
  const char* ZipCmd = "7z.exe a -y -bd -si";
  #else
  const char* ZipCmd = "7za a -y -bd -si";
  #endif
  if (FExtToCmdH.Empty()) {
    FExtToCmdH.AddDat(".gz",  ZipCmd);
    FExtToCmdH.AddDat(".7z",  ZipCmd);
    FExtToCmdH.AddDat(".rar", ZipCmd);
    FExtToCmdH.AddDat(".zip", ZipCmd);
    FExtToCmdH.AddDat(".cab", ZipCmd);
    FExtToCmdH.AddDat(".arj", ZipCmd);
    FExtToCmdH.AddDat(".bzip2", ZipCmd);
    FExtToCmdH.AddDat(".bz2", ZipCmd);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void TZipOut::Flush ( ) [virtual]

Implements TSOut.

Definition at line 392 of file zipfl.cpp.

References EAssertR, FlushBf(), TSBase::GetSNm(), and ZipStdinWr.

                   {
  FlushBf();
  #ifdef GLib_WIN
  EAssertR(FlushFileBuffers(ZipStdinWr)!=0, "Can not flush file '"+GetSNm()+"'.");
  #else
  EAssertR(fflush(ZipStdinWr)==0, "Can not flush file '"+GetSNm()+"'.");
  #endif
}

Here is the call graph for this function:

void TZipOut::FlushBf ( ) [private]

Definition at line 300 of file zipfl.cpp.

References Bf, BfL, EAssert, EAssertR, TSBase::GetSNm(), and ZipStdinWr.

Referenced by Flush(), PutCh(), and ~TZipOut().

                      {
  #ifdef GLib_WIN
  DWORD BytesOut;
  EAssertR(WriteFile(ZipStdinWr, Bf, DWORD(BfL), &BytesOut, NULL)!=0, "Error writting to the file '"+GetSNm()+"'.");
  #else
  size_t BytesOut = fwrite(Bf, 1, BfL, ZipStdinWr);
  #endif
  EAssert(BytesOut == BfL);
  BfL = 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 425 of file zipfl.cpp.

References TStr::CStr(), EAssertR, THash< TKey, TDat, THashFunc >::Empty(), FExtToCmdH, FillFExtToCmdH(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::GetDat(), TStr::GetFExt(), TStr::GetFMid(), TStr::GetLc(), and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by TZipOut().

                                       {
  if (FExtToCmdH.Empty()) FillFExtToCmdH();
  const TStr Ext = ZipFNm.GetFExt().GetLc();
  EAssertR(FExtToCmdH.IsKey(Ext), TStr::Fmt("Unsupported file extension '%s'", Ext.CStr()));
  return FExtToCmdH.GetDat(Ext)+ZipFNm.GetFMid();
}

Here is the call graph for this function:

Here is the caller graph for this function:

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 401 of file zipfl.cpp.

References THash< TKey, TDat, THashFunc >::Empty(), FExtToCmdH, FillFExtToCmdH(), and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by IsZipFNm().

                                         {
  if (FExtToCmdH.Empty()) FillFExtToCmdH();
  return FExtToCmdH.IsKey(FNmExt);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static bool TZipOut::IsZipFNm ( const TStr FNm) [inline, static]

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

Definition at line 97 of file zipfl.h.

References TStr::GetFExt(), and IsZipExt().

Referenced by NewIfZip().

{ return IsZipExt(FNm.GetFExt()); }

Here is the call graph for this function:

Here is the caller graph for this function:

PSOut TZipOut::New ( const TStr FNm) [static]

Definition at line 360 of file zipfl.cpp.

References TZipOut().

Referenced by NewIfZip().

                                 {
  return PSOut(new TZipOut(FNm));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PSOut TZipOut::NewIfZip ( const TStr FNm) [inline, static]

Definition at line 102 of file zipfl.h.

References IsZipFNm(), and New().

{ return IsZipFNm(FNm) ? New(FNm) : TFOut::New(FNm); }

Here is the call graph for this function:

TZipOut& TZipOut::operator= ( const TZipOut ) [private]
int TZipOut::PutBf ( const void *  LBf,
const TSize LBfL 
) [virtual]

Implements TSOut.

Definition at line 380 of file zipfl.cpp.

References Bf, BfL, MxBfL, and PutCh().

                                                    {
  int LBfS=0;
  if (BfL+LBfL>MxBfL){
    for (TSize LBfC=0; LBfC<LBfL; LBfC++){
      LBfS+=PutCh(((char*)LBf)[LBfC]);}
  } else {
    for (TSize LBfC=0; LBfC<LBfL; LBfC++){
      LBfS+=(Bf[BfL++]=((char*)LBf)[LBfC]);}
  }
  return LBfS;
}

Here is the call graph for this function:

int TZipOut::PutCh ( const char &  Ch) [virtual]

Implements TSOut.

Definition at line 375 of file zipfl.cpp.

References Bf, BfL, FlushBf(), and MxBfL.

Referenced by PutBf().

                                {
  if (BfL==MxBfL) {FlushBf();}
  return Bf[BfL++]=Ch;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

char* TZipOut::Bf [private]

Definition at line 77 of file zipfl.h.

Referenced by FlushBf(), PutBf(), PutCh(), TZipOut(), and ~TZipOut().

TSize TZipOut::BfL [private]

Definition at line 78 of file zipfl.h.

Referenced by FlushBf(), PutBf(), PutCh(), TZipOut(), and ~TZipOut().

TStrStrH TZipOut::FExtToCmdH [static, private]

Definition at line 71 of file zipfl.h.

Referenced by FillFExtToCmdH(), GetCmd(), and IsZipExt().

const TSize TZipOut::MxBfL = 4*1024 [static, private]

Definition at line 70 of file zipfl.h.

Referenced by PutBf(), PutCh(), and TZipOut().

FILE* TZipOut::ZipStdinRd [private]

Definition at line 75 of file zipfl.h.

Referenced by CreateZipProcess(), TZipOut(), and ~TZipOut().

FILE * TZipOut::ZipStdinWr [private]

Definition at line 75 of file zipfl.h.

Referenced by CreateZipProcess(), Flush(), FlushBf(), TZipOut(), and ~TZipOut().


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