SNAP Library 2.1, User Reference  2013-09-25 10:47:25
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>

Inherits TSOut.

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'. Note3: Use TZipIn::SevenZipPath to set the path to 7z executable.

Definition at line 72 of file zipfl.h.


Constructor & Destructor Documentation

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

Definition at line 338 of file zipfl.cpp.

                                : 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;
}

Definition at line 361 of file zipfl.cpp.

                  {
  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; }
}

Member Function Documentation

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

Definition at line 305 of file zipfl.cpp.

                                                                  {
  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");
  if (ZipStdinWr == NULL) { // try using SevenZipPath
    ZipStdinWr = popen((TZipIn::SevenZipPath+"/"+CmdLine).CStr(), "r");
  }
  EAssertR(ZipStdinWr != NULL,  TStr::Fmt("Can not execute '%s'", CmdLine.CStr()).CStr());
  #endif
}
void TZipOut::FillFExtToCmdH ( ) [static, private]

Definition at line 403 of file zipfl.cpp.

                             {
   // 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);
  }
}
void TZipOut::Flush ( ) [virtual]

Implements TSOut.

Definition at line 389 of file zipfl.cpp.

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

Definition at line 294 of file zipfl.cpp.

                      {
  #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;
}
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 422 of file zipfl.cpp.

                                       {
  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();
}
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 398 of file zipfl.cpp.

                                         {
  if (FExtToCmdH.Empty()) FillFExtToCmdH();
  return FExtToCmdH.IsKey(FNmExt);
}
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 101 of file zipfl.h.

{ return IsZipExt(FNm.GetFExt()); }
PSOut TZipOut::New ( const TStr FNm) [static]

Definition at line 357 of file zipfl.cpp.

                                 {
  return PSOut(new TZipOut(FNm));
}
static PSOut TZipOut::NewIfZip ( const TStr FNm) [inline, static]

Definition at line 106 of file zipfl.h.

{ return IsZipFNm(FNm) ? New(FNm) : TFOut::New(FNm); }
TZipOut& TZipOut::operator= ( const TZipOut ) [private]
int TZipOut::PutBf ( const void *  LBf,
const TSize LBfL 
) [virtual]

Implements TSOut.

Definition at line 377 of file zipfl.cpp.

                                                    {
  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;
}
int TZipOut::PutCh ( const char &  Ch) [virtual]

Implements TSOut.

Definition at line 372 of file zipfl.cpp.

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

Member Data Documentation

char* TZipOut::Bf [private]

Definition at line 81 of file zipfl.h.

TSize TZipOut::BfL [private]

Definition at line 82 of file zipfl.h.

TStrStrH TZipOut::FExtToCmdH [static, private]

Definition at line 75 of file zipfl.h.

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

Definition at line 74 of file zipfl.h.

FILE* TZipOut::ZipStdinRd [private]

Definition at line 79 of file zipfl.h.

FILE * TZipOut::ZipStdinWr [private]

Definition at line 79 of file zipfl.h.


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