diff src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderOutStream.cpp @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Archive/7z/7zFolderOutStream.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,164 @@
     1.4 +// 7zFolderOutStream.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "7zFolderOutStream.h"
     1.9 +
    1.10 +namespace NArchive {
    1.11 +namespace N7z {
    1.12 +
    1.13 +CFolderOutStream::CFolderOutStream()
    1.14 +{
    1.15 +  _outStreamWithHashSpec = new COutStreamWithCRC;
    1.16 +  _outStreamWithHash = _outStreamWithHashSpec;
    1.17 +}
    1.18 +
    1.19 +HRESULT CFolderOutStream::Init(
    1.20 +    const CArchiveDatabaseEx *archiveDatabase,
    1.21 +    UInt32 ref2Offset,
    1.22 +    UInt32 startIndex,
    1.23 +    const CBoolVector *extractStatuses,
    1.24 +    IArchiveExtractCallback *extractCallback,
    1.25 +    bool testMode,
    1.26 +    bool checkCrc)
    1.27 +{
    1.28 +  _archiveDatabase = archiveDatabase;
    1.29 +  _ref2Offset = ref2Offset;
    1.30 +  _startIndex = startIndex;
    1.31 +
    1.32 +  _extractStatuses = extractStatuses;
    1.33 +  _extractCallback = extractCallback;
    1.34 +  _testMode = testMode;
    1.35 +
    1.36 +  _checkCrc = checkCrc;
    1.37 +
    1.38 +  _currentIndex = 0;
    1.39 +  _fileIsOpen = false;
    1.40 +  return WriteEmptyFiles();
    1.41 +}
    1.42 +
    1.43 +HRESULT CFolderOutStream::OpenFile()
    1.44 +{
    1.45 +  Int32 askMode;
    1.46 +  if((*_extractStatuses)[_currentIndex])
    1.47 +    askMode = _testMode ?
    1.48 +        NArchive::NExtract::NAskMode::kTest :
    1.49 +        NArchive::NExtract::NAskMode::kExtract;
    1.50 +  else
    1.51 +    askMode = NArchive::NExtract::NAskMode::kSkip;
    1.52 +  CMyComPtr<ISequentialOutStream> realOutStream;
    1.53 +
    1.54 +  UInt32 index = _startIndex + _currentIndex;
    1.55 +  RINOK(_extractCallback->GetStream(_ref2Offset + index, &realOutStream, askMode));
    1.56 +
    1.57 +  _outStreamWithHashSpec->SetStream(realOutStream);
    1.58 +  _outStreamWithHashSpec->Init(_checkCrc);
    1.59 +  if (askMode == NArchive::NExtract::NAskMode::kExtract &&
    1.60 +      (!realOutStream))
    1.61 +  {
    1.62 +    const CFileItem &fi = _archiveDatabase->Files[index];
    1.63 +    if (!_archiveDatabase->IsItemAnti(index) && !fi.IsDir)
    1.64 +      askMode = NArchive::NExtract::NAskMode::kSkip;
    1.65 +  }
    1.66 +  return _extractCallback->PrepareOperation(askMode);
    1.67 +}
    1.68 +
    1.69 +HRESULT CFolderOutStream::WriteEmptyFiles()
    1.70 +{
    1.71 +  for(;_currentIndex < _extractStatuses->Size(); _currentIndex++)
    1.72 +  {
    1.73 +    UInt32 index = _startIndex + _currentIndex;
    1.74 +    const CFileItem &fi = _archiveDatabase->Files[index];
    1.75 +    if (!_archiveDatabase->IsItemAnti(index) && !fi.IsDir && fi.Size != 0)
    1.76 +      return S_OK;
    1.77 +    RINOK(OpenFile());
    1.78 +    RINOK(_extractCallback->SetOperationResult(NArchive::NExtract::NOperationResult::kOK));
    1.79 +    _outStreamWithHashSpec->ReleaseStream();
    1.80 +  }
    1.81 +  return S_OK;
    1.82 +}
    1.83 +
    1.84 +STDMETHODIMP CFolderOutStream::Write(const void *data,
    1.85 +    UInt32 size, UInt32 *processedSize)
    1.86 +{
    1.87 +  UInt32 realProcessedSize = 0;
    1.88 +  while(_currentIndex < _extractStatuses->Size())
    1.89 +  {
    1.90 +    if (_fileIsOpen)
    1.91 +    {
    1.92 +      UInt32 index = _startIndex + _currentIndex;
    1.93 +      const CFileItem &fi = _archiveDatabase->Files[index];
    1.94 +      UInt64 fileSize = fi.Size;
    1.95 +      
    1.96 +      UInt32 numBytesToWrite = (UInt32)MyMin(fileSize - _filePos,
    1.97 +          UInt64(size - realProcessedSize));
    1.98 +      
    1.99 +      UInt32 processedSizeLocal;
   1.100 +      RINOK(_outStreamWithHash->Write((const Byte *)data + realProcessedSize,
   1.101 +            numBytesToWrite, &processedSizeLocal));
   1.102 +
   1.103 +      _filePos += processedSizeLocal;
   1.104 +      realProcessedSize += processedSizeLocal;
   1.105 +      if (_filePos == fileSize)
   1.106 +      {
   1.107 +        bool digestsAreEqual;
   1.108 +        if (fi.CrcDefined && _checkCrc)
   1.109 +          digestsAreEqual = fi.Crc == _outStreamWithHashSpec->GetCRC();
   1.110 +        else
   1.111 +          digestsAreEqual = true;
   1.112 +
   1.113 +        RINOK(_extractCallback->SetOperationResult(
   1.114 +            digestsAreEqual ?
   1.115 +            NArchive::NExtract::NOperationResult::kOK :
   1.116 +            NArchive::NExtract::NOperationResult::kCRCError));
   1.117 +        _outStreamWithHashSpec->ReleaseStream();
   1.118 +        _fileIsOpen = false;
   1.119 +        _currentIndex++;
   1.120 +      }
   1.121 +      if (realProcessedSize == size)
   1.122 +      {
   1.123 +        if (processedSize != NULL)
   1.124 +          *processedSize = realProcessedSize;
   1.125 +        return WriteEmptyFiles();
   1.126 +      }
   1.127 +    }
   1.128 +    else
   1.129 +    {
   1.130 +      RINOK(OpenFile());
   1.131 +      _fileIsOpen = true;
   1.132 +      _filePos = 0;
   1.133 +    }
   1.134 +  }
   1.135 +  if (processedSize != NULL)
   1.136 +    *processedSize = size;
   1.137 +  return S_OK;
   1.138 +}
   1.139 +
   1.140 +HRESULT CFolderOutStream::FlushCorrupted(Int32 resultEOperationResult)
   1.141 +{
   1.142 +  while(_currentIndex < _extractStatuses->Size())
   1.143 +  {
   1.144 +    if (_fileIsOpen)
   1.145 +    {
   1.146 +      RINOK(_extractCallback->SetOperationResult(resultEOperationResult));
   1.147 +      _outStreamWithHashSpec->ReleaseStream();
   1.148 +      _fileIsOpen = false;
   1.149 +      _currentIndex++;
   1.150 +    }
   1.151 +    else
   1.152 +    {
   1.153 +      RINOK(OpenFile());
   1.154 +      _fileIsOpen = true;
   1.155 +    }
   1.156 +  }
   1.157 +  return S_OK;
   1.158 +}
   1.159 +
   1.160 +HRESULT CFolderOutStream::WasWritingFinished()
   1.161 +{
   1.162 +  if (_currentIndex == _extractStatuses->Size())
   1.163 +    return S_OK;
   1.164 +  return E_FAIL;
   1.165 +}
   1.166 +
   1.167 +}}