Esempio n. 1
0
int Main2(
#ifndef _WIN32
    int numArgs, char *args[]
#endif
)
{
#if defined(_WIN32) && !defined(UNDER_CE)
    SetFileApisToOEM();
#endif

    g_StdOut << kCopyrightString;

    UStringVector commandStrings;
#ifdef _WIN32
    NCommandLineParser::SplitCommandLine(GetCommandLineW(), commandStrings);
#else
    // GetArguments(numArgs, args, commandStrings);
    mySplitCommandLine(numArgs,args,commandStrings);
#endif

#ifdef _WIN32

    FString arcPath;
    {
        FString path;
        NDLL::MyGetModuleFileName(path);
        if (!MyGetFullPathName(path, arcPath))
        {
            g_StdOut << "GetFullPathName Error";
            return NExitCode::kFatalError;
        }
    }

#else
    // After mySplitCommandLine
    showP7zipInfo(&g_StdOut);

    UString arcPath = commandStrings.Front();

#endif

    commandStrings.Delete(0);

    NCommandLineParser::CParser parser(kNumSwitches);
    try
    {
        parser.ParseStrings(kSwitchForms, commandStrings);
    }
    catch(...)
    {
        PrintHelpAndExit();
    }

    if (parser[NKey::kHelp1].ThereIs || parser[NKey::kHelp2].ThereIs)
    {
        PrintHelp();
        return 0;
    }
    const UStringVector &nonSwitchStrings = parser.NonSwitchStrings;

    int numNonSwitchStrings = nonSwitchStrings.Size();

    CArchiveCommand command;
    if (numNonSwitchStrings == 0)
        command.CommandType = NCommandType::kFullExtract;
    else
    {
        if (numNonSwitchStrings > 1)
            PrintHelpAndExit();
        if (!ParseArchiveCommand(nonSwitchStrings[kCommandIndex], command))
            PrintHelpAndExit();
    }


    NRecursedType::EEnum recursedType;
    recursedType = command.DefaultRecursedType();

    NWildcard::CCensor wildcardCensor;

    bool thereAreSwitchIncludeWildcards;
    thereAreSwitchIncludeWildcards = false;

    AddToCensorFromNonSwitchesStrings(wildcardCensor, nonSwitchStrings, recursedType,
                                      thereAreSwitchIncludeWildcards);

    bool yesToAll = parser[NKey::kYes].ThereIs;

    // NExtractMode::EEnum extractMode;
    // bool isExtractGroupCommand = command.IsFromExtractGroup(extractMode);

    bool passwordEnabled = parser[NKey::kPassword].ThereIs;

    UString password;
    if (passwordEnabled)
        password = parser[NKey::kPassword].PostStrings[0];

    if (!NFind::DoesFileExist(arcPath))
        throw kCantFindSFX;

    FString outputDir;
    if (parser[NKey::kOutputDir].ThereIs)
    {
        outputDir = us2fs(parser[NKey::kOutputDir].PostStrings[0]);
        NName::NormalizeDirPathPrefix(outputDir);
    }


    wildcardCensor.AddPathsToCensor(NWildcard::k_RelatPath);

    {
        UStringVector v1, v2;
        v1.Add(fs2us(arcPath));
        v2.Add(fs2us(arcPath));
        const NWildcard::CCensorNode &wildcardCensorHead =
            wildcardCensor.Pairs.Front().Head;

        CCodecs *codecs = new CCodecs;
        CMyComPtr<
#ifdef EXTERNAL_CODECS
        ICompressCodecsInfo
#else
        IUnknown
#endif
        > compressCodecsInfo = codecs;
        {
            HRESULT result = codecs->Load();
            if (result != S_OK)
                throw CSystemException(result);
        }

        if (command.CommandType != NCommandType::kList)
        {
            CExtractCallbackConsole *ecs = new CExtractCallbackConsole;
            CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
            ecs->Init(g_StdStream, &g_StdErr, g_StdStream);

#ifndef _NO_CRYPTO
            ecs->PasswordIsDefined = passwordEnabled;
            ecs->Password = password;
#endif

            /*
            COpenCallbackConsole openCallback;
            openCallback.Init(g_StdStream, g_StdStream);

            #ifndef _NO_CRYPTO
            openCallback.PasswordIsDefined = passwordEnabled;
            openCallback.Password = password;
            #endif
            */

            CExtractOptions eo;
            eo.StdOutMode = false;
            eo.YesToAll = yesToAll;
            eo.TestMode = command.CommandType == NCommandType::kTest;
            eo.PathMode = NExtract::NPathMode::kFullPaths;
            eo.OverwriteMode = yesToAll ?
                               NExtract::NOverwriteMode::kOverwrite :
                               NExtract::NOverwriteMode::kAsk;
            eo.OutputDir = outputDir;

            UString errorMessage;
            CDecompressStat stat;
            HRESULT result = Extract(
                                 codecs, CObjectVector<COpenType>(), CIntVector(),
                                 v1, v2,
                                 wildcardCensorHead,
                                 eo, ecs, ecs,
                                 // NULL, // hash
                                 errorMessage, stat);
            if (!errorMessage.IsEmpty())
            {
                (*g_StdStream) << endl << "Error: " << errorMessage;;
                if (result == S_OK)
                    result = E_FAIL;
            }

            if (ecs->NumArcsWithError != 0 || ecs->NumFileErrors != 0)
            {
                if (ecs->NumArcsWithError != 0)
                    (*g_StdStream) << endl << "Archive Errors" << endl;
                if (ecs->NumFileErrors != 0)
                    (*g_StdStream) << endl << "Sub items Errors: " << ecs->NumFileErrors << endl;
                return NExitCode::kFatalError;
            }
            if (result != S_OK)
                throw CSystemException(result);
        }
        else
        {
            throw CSystemException(E_NOTIMPL);

            /*
            UInt64 numErrors = 0;
            UInt64 numWarnings = 0;
            HRESULT result = ListArchives(
                codecs, CObjectVector<COpenType>(), CIntVector(),
                false, // stdInMode
                v1, v2,
                true, // processAltStreams
                false, // showAltStreams
                wildcardCensorHead,
                true, // enableHeaders
                false, // techMode
                #ifndef _NO_CRYPTO
                passwordEnabled, password,
                #endif
                numErrors, numWarnings);
            if (numErrors > 0)
            {
              g_StdOut << endl << "Errors: " << numErrors;
              return NExitCode::kFatalError;
            }
            if (result != S_OK)
              throw CSystemException(result);
            */
        }
    }
    return 0;
}
Esempio n. 2
0
File: Main.cpp Progetto: bks/qz7
int Main2(
  #ifndef _WIN32
  int numArguments, const char *arguments[]
  #endif
)
{
  #ifdef _WIN32
  SetFileApisToOEM();
  #endif
  
  g_StdOut << kCopyrightString;

  UStringVector commandStrings;
  #ifdef _WIN32
  NCommandLineParser::SplitCommandLine(GetCommandLineW(), commandStrings);
  #else
  GetArguments(numArguments, arguments, commandStrings);
  #endif

  UString archiveName = commandStrings.Front();

  commandStrings.Delete(0);

  NCommandLineParser::CParser parser(kNumSwitches);
  try
  {
    parser.ParseStrings(kSwitchForms, commandStrings);
  }
  catch(...)
  {
    PrintHelpAndExit();
  }

  if(parser[NKey::kHelp1].ThereIs || parser[NKey::kHelp2].ThereIs)
  {
    PrintHelp();
    return 0;
  }
  const UStringVector &nonSwitchStrings = parser.NonSwitchStrings;

  int numNonSwitchStrings = nonSwitchStrings.Size();

  CArchiveCommand command;
  if (numNonSwitchStrings == 0)
    command.CommandType = NCommandType::kFullExtract;
  else
  {
    if (numNonSwitchStrings > 1)
      PrintHelpAndExit();
    if (!ParseArchiveCommand(nonSwitchStrings[kCommandIndex], command))
      PrintHelpAndExit();
  }


  NRecursedType::EEnum recursedType;
  recursedType = command.DefaultRecursedType();

  NWildcard::CCensor wildcardCensor;
  
  bool thereAreSwitchIncludeWildCards;
  thereAreSwitchIncludeWildCards = false;
  AddToCensorFromNonSwitchesStrings(wildcardCensor, nonSwitchStrings, recursedType,
      thereAreSwitchIncludeWildCards);

  bool yesToAll = parser[NKey::kYes].ThereIs;

  #ifdef _WIN32
  if (archiveName.Right(kDefaultExtLength).CompareNoCase(kDefaultExt) != 0)
    archiveName += kDefaultExt;
  #endif

  // NExtractMode::EEnum extractMode;
  // bool isExtractGroupCommand = command.IsFromExtractGroup(extractMode);

  bool passwordEnabled = parser[NKey::kPassword].ThereIs;

  UString password;
  if(passwordEnabled)
    password = parser[NKey::kPassword].PostStrings[0];

  NFind::CFileInfoW archiveFileInfo;
  if (!NFind::FindFile(archiveName, archiveFileInfo))
    throw kCantFindSFX;
  if (archiveFileInfo.IsDir())
    throw kCantFindSFX;
  
  UString outputDir;
  if(parser[NKey::kOutputDir].ThereIs)
  {
    outputDir = parser[NKey::kOutputDir].PostStrings[0];
    NName::NormalizeDirPathPrefix(outputDir);
  }

  {
    UStringVector v1, v2;
    v1.Add(archiveName);
    v2.Add(archiveName);
    const NWildcard::CCensorNode &wildcardCensorHead =
      wildcardCensor.Pairs.Front().Head;

    CCodecs *codecs = new CCodecs;
    CMyComPtr<
      #ifdef EXTERNAL_CODECS
      ICompressCodecsInfo
      #else
      IUnknown
      #endif
      > compressCodecsInfo = codecs;
    HRESULT result = codecs->Load();
    if (result != S_OK)
      throw CSystemException(result);

    if(command.CommandType != NCommandType::kList)
    {
      CExtractCallbackConsole *ecs = new CExtractCallbackConsole;
      CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
      ecs->OutStream = g_StdStream;
      ecs->PasswordIsDefined = passwordEnabled;
      ecs->Password = password;
      ecs->Init();

      COpenCallbackConsole openCallback;
      openCallback.OutStream = g_StdStream;
      openCallback.PasswordIsDefined = passwordEnabled;
      openCallback.Password = password;

      CExtractOptions eo;
      eo.StdOutMode = false;
      eo.PathMode = NExtract::NPathMode::kFullPathnames;
      eo.TestMode = command.CommandType == NCommandType::kTest;
      eo.OverwriteMode = yesToAll ?
          NExtract::NOverwriteMode::kWithoutPrompt :
          NExtract::NOverwriteMode::kAskBefore;
      eo.OutputDir = outputDir;
      eo.YesToAll = yesToAll;

      UString errorMessage;
      CDecompressStat stat;
      HRESULT result = DecompressArchives(
          codecs, CIntVector(),
          v1, v2,
          wildcardCensorHead,
          eo, &openCallback, ecs, errorMessage, stat);
      if (!errorMessage.IsEmpty())
      {
        (*g_StdStream) << endl << "Error: " << errorMessage;;
        if (result == S_OK)
          result = E_FAIL;
      }

      if (ecs->NumArchiveErrors != 0 || ecs->NumFileErrors != 0)
      {
        if (ecs->NumArchiveErrors != 0)
          (*g_StdStream) << endl << "Archive Errors: " << ecs->NumArchiveErrors << endl;
        if (ecs->NumFileErrors != 0)
          (*g_StdStream) << endl << "Sub items Errors: " << ecs->NumFileErrors << endl;
        return NExitCode::kFatalError;
      }
      if (result != S_OK)
        throw CSystemException(result);
    }
    else
    {
      UInt64 numErrors = 0;
      HRESULT result = ListArchives(
          codecs, CIntVector(),
          v1, v2,
          wildcardCensorHead,
          true, false,
          passwordEnabled,
          password, numErrors);
      if (numErrors > 0)
      {
        g_StdOut << endl << "Errors: " << numErrors;
        return NExitCode::kFatalError;
      }
      if (result != S_OK)
        throw CSystemException(result);
    }
  }
  return 0;
}
int Main2(
  #ifndef _WIN32
  int numArguments, const char *arguments[]
  #endif
)
{
  #ifdef _WIN32
  SetFileApisToOEM();
  #endif
  
  UStringVector commandStrings;
  #ifdef _WIN32
  NCommandLineParser::SplitCommandLine(GetCommandLineW(), commandStrings);
  #else
  extern void mySplitCommandLine(int numArguments,const char *arguments[],UStringVector &parts);
  mySplitCommandLine(numArguments,arguments,commandStrings);
  #endif

  // After mySplitCommandLine
  g_StdOut << kCopyrightString << " (locale=" << my_getlocale() <<",Utf16=";
  if (global_use_utf16_conversion) g_StdOut << "on";
  else                             g_StdOut << "off";
  g_StdOut << ",HugeFiles=";
  if (sizeof(off_t) >= 8) g_StdOut << "on,";
  else                    g_StdOut << "off,";
  int nbcpu = NWindows::NSystem::GetNumberOfProcessors();
  if (nbcpu > 1) g_StdOut << nbcpu << " CPUs)\n";
  else           g_StdOut << nbcpu << " CPU)\n";
 
  UString arcPath = commandStrings.Front();

  commandStrings.Delete(0);

  NCommandLineParser::CParser parser(kNumSwitches);
  try
  {
    parser.ParseStrings(kSwitchForms, commandStrings);
  }
  catch(...)
  {
    PrintHelpAndExit();
  }

  if(parser[NKey::kHelp1].ThereIs || parser[NKey::kHelp2].ThereIs)
  {
    PrintHelp();
    return 0;
  }
  const UStringVector &nonSwitchStrings = parser.NonSwitchStrings;

  int numNonSwitchStrings = nonSwitchStrings.Size();

  CArchiveCommand command;
  if (numNonSwitchStrings == 0)
    command.CommandType = NCommandType::kFullExtract;
  else
  {
    if (numNonSwitchStrings > 1)
      PrintHelpAndExit();
    if (!ParseArchiveCommand(nonSwitchStrings[kCommandIndex], command))
      PrintHelpAndExit();
  }


  NRecursedType::EEnum recursedType;
  recursedType = command.DefaultRecursedType();

  NWildcard::CCensor wildcardCensor;
  
  bool thereAreSwitchIncludeWildCards;
  thereAreSwitchIncludeWildCards = false;
  AddToCensorFromNonSwitchesStrings(wildcardCensor, nonSwitchStrings, recursedType,
      thereAreSwitchIncludeWildCards);

  bool yesToAll = parser[NKey::kYes].ThereIs;

  // NExtractMode::EEnum extractMode;
  // bool isExtractGroupCommand = command.IsFromExtractGroup(extractMode);

  bool passwordEnabled = parser[NKey::kPassword].ThereIs;

  UString password;
  if(passwordEnabled)
    password = parser[NKey::kPassword].PostStrings[0];

  if (!NFind::DoesFileExist(arcPath))
    throw kCantFindSFX;
  
  UString outputDir;
  if (parser[NKey::kOutputDir].ThereIs)
  {
    outputDir = parser[NKey::kOutputDir].PostStrings[0];
    NName::NormalizeDirPathPrefix(outputDir);
  }

  {
    UStringVector v1, v2;
    v1.Add(arcPath);
    v2.Add(arcPath);
    const NWildcard::CCensorNode &wildcardCensorHead =
      wildcardCensor.Pairs.Front().Head;

    CCodecs *codecs = new CCodecs;
    CMyComPtr<
      #ifdef EXTERNAL_CODECS
      ICompressCodecsInfo
      #else
      IUnknown
      #endif
      > compressCodecsInfo = codecs;
    HRESULT result = codecs->Load();
    if (result != S_OK)
      throw CSystemException(result);

    if(command.CommandType != NCommandType::kList)
    {
      CExtractCallbackConsole *ecs = new CExtractCallbackConsole;
      CMyComPtr<IFolderArchiveExtractCallback> extractCallback = ecs;
      ecs->OutStream = g_StdStream;

      #ifndef _NO_CRYPTO
      ecs->PasswordIsDefined = passwordEnabled;
      ecs->Password = password;
      #endif

      ecs->Init();

      COpenCallbackConsole openCallback;
      openCallback.OutStream = g_StdStream;

      #ifndef _NO_CRYPTO
      openCallback.PasswordIsDefined = passwordEnabled;
      openCallback.Password = password;
      #endif

      CExtractOptions eo;
      eo.StdOutMode = false;
      eo.PathMode = NExtract::NPathMode::kFullPathnames;
      eo.TestMode = command.CommandType == NCommandType::kTest;
      eo.OverwriteMode = yesToAll ?
          NExtract::NOverwriteMode::kWithoutPrompt :
          NExtract::NOverwriteMode::kAskBefore;
      eo.OutputDir = outputDir;
      eo.YesToAll = yesToAll;

      UString errorMessage;
      CDecompressStat stat;
      HRESULT result = DecompressArchives(
          codecs, CIntVector(),
          v1, v2,
          wildcardCensorHead,
          eo, &openCallback, ecs, errorMessage, stat);
      if (!errorMessage.IsEmpty())
      {
        (*g_StdStream) << endl << "Error: " << errorMessage;;
        if (result == S_OK)
          result = E_FAIL;
      }

      if (ecs->NumArchiveErrors != 0 || ecs->NumFileErrors != 0)
      {
        if (ecs->NumArchiveErrors != 0)
          (*g_StdStream) << endl << "Archive Errors: " << ecs->NumArchiveErrors << endl;
        if (ecs->NumFileErrors != 0)
          (*g_StdStream) << endl << "Sub items Errors: " << ecs->NumFileErrors << endl;
        return NExitCode::kFatalError;
      }
      if (result != S_OK)
        throw CSystemException(result);
    }
    else
    {
      UInt64 numErrors = 0;
      HRESULT result = ListArchives(
          codecs, CIntVector(),
          false,
          v1, v2,
          wildcardCensorHead,
          true, false,
          #ifndef _NO_CRYPTO
          passwordEnabled, password,
          #endif
          numErrors);
      if (numErrors > 0)
      {
        g_StdOut << endl << "Errors: " << numErrors;
        return NExitCode::kFatalError;
      }
      if (result != S_OK)
        throw CSystemException(result);
    }
  }
  return 0;
}