bool CZipArchive::WriteNewFile(void *pBuf, DWORD iSize) { if (m_iFileOpened != compress) { TRACE(_T("A new file must be opened.\n")); return false; } m_info.m_stream.next_in = (unsigned char*)pBuf; m_info.m_stream.avail_in = iSize; CurrentFile()->m_uCrc32 = crc32(CurrentFile()->m_uCrc32, (unsigned char*)pBuf, iSize); while (m_info.m_stream.avail_in > 0) { if (m_info.m_stream.avail_out == 0) { m_storage.Write(m_info.m_pBuffer, m_info.m_uComprLeft, false); m_info.m_uComprLeft = 0; m_info.m_stream.avail_out = m_info.m_pBuffer.GetSize(); m_info.m_stream.next_out = (unsigned char*)(char*)m_info.m_pBuffer; } if (CurrentFile()->m_uMethod == Z_DEFLATED) { DWORD uTotal = m_info.m_stream.total_out; int err = deflate(&m_info.m_stream, Z_NO_FLUSH); CheckForError(err); m_info.m_uComprLeft += m_info.m_stream.total_out - uTotal; } else { DWORD uToCopy = (m_info.m_stream.avail_in < m_info.m_stream.avail_out) ? m_info.m_stream.avail_in : m_info.m_stream.avail_out; memcpy(m_info.m_stream.next_out, m_info.m_stream.next_in, uToCopy); m_info.m_stream.avail_in -= uToCopy; m_info.m_stream.avail_out -= uToCopy; m_info.m_stream.next_in += uToCopy; m_info.m_stream.next_out += uToCopy; m_info.m_stream.total_in += uToCopy; m_info.m_stream.total_out += uToCopy; m_info.m_uComprLeft += uToCopy; } } return true; }
void CZipArchive::SetExtraField(char *pBuf, WORD iSize) { if (m_iFileOpened != compress) { TRACE(_T("A new file must be opened.\n")); return; } if (!pBuf || !iSize) return; CurrentFile()->m_pExtraField.Allocate(iSize); memcpy(CurrentFile()->m_pExtraField, pBuf, iSize); CurrentFile()->m_uExtraFieldSize = iSize; }
bool CZipArchive::CloseNewFile() { if (m_iFileOpened != compress) { TRACE(_T("A new file must be opened.\n")); return false; } m_info.m_stream.avail_in = 0; int err = Z_OK; if (CurrentFile()->m_uMethod == Z_DEFLATED) while (err == Z_OK) { if (m_info.m_stream.avail_out == 0) { m_storage.Write(m_info.m_pBuffer, m_info.m_uComprLeft, false); m_info.m_uComprLeft = 0; m_info.m_stream.avail_out = m_info.m_pBuffer.GetSize(); m_info.m_stream.next_out = (unsigned char*)(char*)m_info.m_pBuffer; } DWORD uTotal = m_info.m_stream.total_out; err = deflate(&m_info.m_stream, Z_FINISH); m_info.m_uComprLeft += m_info.m_stream.total_out - uTotal; } if (err == Z_STREAM_END) err = Z_OK; CheckForError(err); if (m_info.m_uComprLeft > 0) m_storage.Write(m_info.m_pBuffer, m_info.m_uComprLeft, false); if (CurrentFile()->m_uMethod == Z_DEFLATED) { err = deflateEnd(&m_info.m_stream); CheckForError(err); } CurrentFile()->m_uComprSize = m_info.m_stream.total_out; CurrentFile()->m_uUncomprSize = m_info.m_stream.total_in; m_centralDir.CloseNewFile(); m_iFileOpened = nothing; m_info.m_pBuffer.Release(); return true; }
bool CZipArchive::OpenFile(WORD uIndex) { if (!m_centralDir.IsValidIndex(uIndex)) return false; if (m_storage.IsSpanMode() == 1) { TRACE(_T("You cannot extract from the span in creation.\n")); return false; } if (m_iFileOpened) { TRACE(_T("A file already opened.\n")); return false; } m_info.Init(); m_centralDir.OpenFile(uIndex); WORD uMethod = CurrentFile()->m_uMethod; if ((uMethod != 0) &&(uMethod != Z_DEFLATED)) ThrowError(ZIP_BADZIPFILE); if (uMethod == Z_DEFLATED) { m_info.m_stream.opaque = m_bDetectZlibMemoryLeaks ? &m_list : 0; int err = inflateInit2(&m_info.m_stream, -MAX_WBITS); // * windowBits is passed < 0 to tell that there is no zlib header. // * Note that in this case inflate *requires* an extra "dummy" byte // * after the compressed stream in order to complete decompression and // * return Z_STREAM_END. CheckForError(err); } m_info.m_uComprLeft = CurrentFile()->m_uComprSize; m_info.m_uUncomprLeft = CurrentFile()->m_uUncomprSize; m_info.m_uCrc32 = 0; m_info.m_stream.total_out = 0; m_info.m_stream.avail_in = 0; m_iFileOpened = extract; return true; }
bool CZipArchive::CloseFile(LPCTSTR lpszFilePath) { if (m_iFileOpened != extract) { TRACE(_T("No file opened.\n")); return false; } if (m_info.m_uUncomprLeft == 0) { if (m_info.m_uCrc32 != CurrentFile()->m_uCrc32) ThrowError(ZIP_BADCRC); } if (CurrentFile()->m_uMethod == Z_DEFLATED) inflateEnd(&m_info.m_stream); bool bRet = true; if (lpszFilePath) { try { CFileStatus fs; fs.m_ctime = fs.m_atime = CTime::GetCurrentTime(); fs.m_attribute = 0; fs.m_mtime = CurrentFile()->GetTime(); CFile::SetStatus(lpszFilePath, fs); bRet = SetFileAttributes(lpszFilePath, CurrentFile()->m_uExternalAttr) != 0; } catch (CException* e) { e->Delete(); return false; } } m_centralDir.CloseFile(); m_iFileOpened = nothing; m_info.m_pBuffer.Release(); return bRet; }
void MainWindow::SaveFile(const QString& file){ setStatusTip(UTF8("保存当前进度到:")+file); QFile fp(file); fp.open(QFile::ReadOnly); QTextStream CurrentFile(&fp); QString OData; QTextStream SaveingFile(&OData); SaveingFile.setCodec("UTF-8"); SaveingFile << StartIndent <<endl; SaveingFile <<endl; SaveingFile<< "[GLOBAL]" <<endl; SaveingFile<<"\tSCREENHEIGHT: "<<singleLine( CurrentSetting->property("screenheight").toString() )<<endl; SaveingFile<<"\tSCREENWIDTH: "<<singleLine( CurrentSetting->property("screenwidth").toString() )<<endl; SaveingFile<<"\tURL: "<<singleLine( CurrentSetting->property("url").toString() )<<endl; SaveingFile<<"\tTEMPLATE: "<<singleLine( CurrentSetting->property("template").toString() )<<endl; SaveingFile<<"\tNAMESPACE: "<<singleLine( CurrentSetting->property("namespace").toString() )<<endl; SaveingFile<<"\tBGSELECTOR: "<<singleLine( CurrentSetting->property("bgselector").toString() )<<endl; SaveingFile<<"\tGENERATEBGIMAGE: "<<singleLine( CurrentSetting->property("generatebgimage").toString() )<<endl; SaveingFile<<"\tLASTPNG: "<<singleLine( CurrentSetting->property("lastpng").toString() )<<endl; SaveingFile<<"[END]"<<endl; SaveingFile <<endl; SaveingFile << wm->SessionSave(); SaveingFile << endl << EndIndent <<endl; //读取文件中的其他部分 QString Line; bool switcher = false; while( !CurrentFile.atEnd() ){ Line = CurrentFile.readLine(); if( !switcher ){ //非 配置or导出 部分 if(Line.startsWith(StartIndent) || Line.startsWith(AutoGenIndent)){ switcher = true; continue; } SaveingFile<<Line<<endl; }else{ //配置or导出 部分 switcher = !(Line.startsWith(EndIndent) || Line.startsWith(AutoGenEndIndent)); } } fp.close(); if(!fp.open(QFile::WriteOnly)) throw AlertException(UTF8("保存失败"),UTF8("无法写入文件")); CurrentFile.reset(); CurrentFilePath = file; this->setWindowTitle(file+QString(" - cssSprites")); qDebug()<<"File Save"<<file; CurrentFile<<OData; fp.close(); QDir fsd; QString target = fsd.absoluteFilePath(CurrentFilePath); target += STUFF_FOLDER; if(fsd.currentPath() != target){ //转移文件 fsd.setPath(target); if( !fsd.exists() ){ if(! fsd.mkdir(target) ) throw AlertException(UTF8("保存错误"),("目标路径无法写入(请检查权限)\n")+target); } QStringList stuff = QDir::current().entryList(QDir::Files); QString fName; foreach(fName,stuff){ qWarning(QString("Copy File ================\n\t\tfrom %1(%2)\n\t\t to %3").arg(fName).arg(QDir::currentPath()).arg(target+fName).toStdString().data()); QFile::copy(fName,target+fName); }
void MainWindow::OpenFile(const QString& file){ setStatusTip(UTF8("打开文件:")+file); bool test_ok = false; QFile fp(file); test_ok = fp.exists(); if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("文件不存在:\n")+file); test_ok = fp.open(QFile::ReadOnly); if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("无法读取文件:\n")+file); QTextStream CurrentFile(&fp); //读取文件中的配置 QString Line; QString OData; QTextStream ProjectData(&OData); bool switcher = false; while( !CurrentFile.atEnd() ){ Line = CurrentFile.readLine(); if( !switcher ){ //非 配置or导出 部分 if(Line.startsWith(StartIndent) || Line.startsWith(AutoGenIndent)){ switcher = true; continue; } }else{ //配置or导出 部分 switcher = !(Line.startsWith(EndIndent) || Line.startsWith(AutoGenEndIndent)); ProjectData<<Line<<endl; } } bool start = false; while(!ProjectData.atEnd()){ QString Line = ProjectData.readLine(); if( Line == "[GLOBAL]" ){ start = true; continue; } if( Line == "[END]" ){ start = false; continue; } if(!start) continue; Line = Line.trimmed(); QString ID = Line.section(' ',0,0).toLower(); ID.truncate(ID.length()-1); QString Value = multiLine( Line.section(' ',1) ); CurrentSetting->setProperty(ID.toStdString().data(),Value); } if(!test_ok) throw AlertException(UTF8("打开失败"),UTF8("无法在文件中识别本程序的格式")); ProjectData.reset(); wm->restart(); //移动当前目录 QString stuff_dir = file + STUFF_FOLDER; QDir stuff(stuff_dir); if( !stuff.exists() ){ stuff.mkdir(stuff_dir); } stuff.setCurrent(stuff_dir); qDebug()<<"[OPEN FILE]Current dir is "<<QDir::currentPath(); wm->SessionResume(OData); CurrentFilePath = file; this->setWindowTitle(file+QString(" - cssSprites")); }
bool CZipArchive::OpenNewFile(CFileHeader & header, int iLevel, LPCTSTR lpszFilePath) { if (IsClosed()) { TRACE(_T("ZipArchive is closed.\n")); return false; } if (m_iFileOpened) { TRACE(_T("A file already opened.\n")); return false; } if (m_storage.IsSpanMode() == -1) { TRACE(_T("You cannot add the files to the existing disk spannig archive.\n")); return false; } if (GetNoEntries() ==(WORD)USHRT_MAX) { TRACE(_T("Maximum file count inside archive reached.\n")); return false; } bool bRet = true; if (lpszFilePath) { CFileStatus fs; if (!CFile::GetStatus(lpszFilePath, fs)) bRet = false; else { header.SetTime(fs.m_mtime); header.m_uExternalAttr = ::GetFileAttributes(lpszFilePath); // mfc bug: m_attribute is 1-byte if (header.m_uExternalAttr == -1) { header.m_uExternalAttr = FILE_ATTRIBUTE_ARCHIVE; bRet = false; } } } m_info.Init(); m_centralDir.AddNewFile(header); CString* pszFileName = &(CurrentFile()->m_szFileName); if (m_bSlashChange) pszFileName->Replace(_T('\\'), _T('/')); //if (m_bOemCompatible) // pszFileName->AnsiToOem(); if (pszFileName->IsEmpty()) pszFileName->Format(_T("file%i"), GetNoEntries()); if (!CurrentFile()->PrepareData(iLevel, m_storage.IsSpanMode() == 1)) ThrowError(ZIP_TOOLONGFILENAME); CurrentFile()->WriteLocal(m_storage); m_info.m_uComprLeft = 0; m_info.m_stream.avail_in = (uInt)0; m_info.m_stream.avail_out = (uInt)m_info.m_pBuffer.GetSize(); m_info.m_stream.next_out = (unsigned char*)(char*)m_info.m_pBuffer; m_info.m_stream.total_in = 0; m_info.m_stream.total_out = 0; if (IsDirectory(CurrentFile()->m_uExternalAttr) &&(CurrentFile()->m_uMethod != 0)) CurrentFile()->m_uMethod = 0; if (CurrentFile()->m_uMethod == Z_DEFLATED) { m_info.m_stream.opaque = m_bDetectZlibMemoryLeaks ? &m_list : 0; int err = deflateInit2(&m_info.m_stream, iLevel, Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); CheckForError(err); } m_iFileOpened = compress; return bRet; }
DWORD CZipArchive::ReadFile(void *pBuf, DWORD iSize) { if (m_iFileOpened != extract) { TRACE(_T("Current file must be opened.\n")); return 0; } if (!pBuf || !iSize) return 0; m_info.m_stream.next_out = (Bytef*)pBuf; m_info.m_stream.avail_out = iSize > m_info.m_uUncomprLeft ? m_info.m_uUncomprLeft : iSize; DWORD iRead = 0; while (m_info.m_stream.avail_out > 0) { if ((m_info.m_stream.avail_in == 0) && (m_info.m_uComprLeft > 0)) { DWORD uToRead = m_info.m_pBuffer.GetSize(); if (m_info.m_uComprLeft < uToRead) uToRead = m_info.m_uComprLeft; if (uToRead == 0) return 0; m_storage.Read(m_info.m_pBuffer, uToRead, false); m_info.m_uComprLeft -= uToRead; m_info.m_stream.next_in = (unsigned char*)(char*)m_info.m_pBuffer; m_info.m_stream.avail_in = uToRead; } if (CurrentFile()->m_uMethod == 0) { DWORD uToCopy = m_info.m_stream.avail_out < m_info.m_stream.avail_in ? m_info.m_stream.avail_out : m_info.m_stream.avail_in; memcpy(m_info.m_stream.next_out, m_info.m_stream.next_in, uToCopy); m_info.m_uCrc32 = crc32(m_info.m_uCrc32, m_info.m_stream.next_out, uToCopy); m_info.m_uUncomprLeft -= uToCopy; m_info.m_stream.avail_in -= uToCopy; m_info.m_stream.avail_out -= uToCopy; m_info.m_stream.next_out += uToCopy; m_info.m_stream.next_in += uToCopy; m_info.m_stream.total_out += uToCopy; iRead += uToCopy; } else { DWORD uTotal = m_info.m_stream.total_out; Bytef* pOldBuf = m_info.m_stream.next_out; int err = inflate(&m_info.m_stream, Z_SYNC_FLUSH); DWORD uToCopy = m_info.m_stream.total_out - uTotal; m_info.m_uCrc32 = crc32(m_info.m_uCrc32, pOldBuf, uToCopy); m_info.m_uUncomprLeft -= uToCopy; iRead += uToCopy; if (err == Z_STREAM_END) return iRead; CheckForError(err); } } return iRead; }