Coding/CPP 삽질기2010. 1. 28. 00:18

//============================================================================
// Name        : ChoboSplit.cpp
// Author      : chobocho.com
// Version     : v0.011
// CopyLeft
// Description : File Split
// Date        : 2010. 10. 28
//============================================================================

#include <iostream>
#include <fstream>
using namespace std;

#define _10MB (10*1024*1024)

class CSplit {
public:
    CSplit();
    ~CSplit();
    bool split(char *fileName);
    void make_batch_file (char *fileName, int count);
private:
    fstream source_fp;
    fstream target_fp;
};

CSplit::CSplit()
{

}

CSplit::~CSplit()
{
    source_fp.close();
    target_fp.close();
}


bool CSplit::split(char *fileName)
{
    int nof_target = 0;
    int count = 0;
    int read_size = 8192;
    char buffer[8192];

    string target_filename = fileName;
    target_filename += ".sh";

    source_fp.open(fileName, ios::binary|ios::in);
    target_fp.open(target_filename.c_str(), ios::binary|ios::out|ios::trunc);
    if (!source_fp)
    {
       cout << "Error : Can't find " << fileName << " !!" << endl;
       return false;
    }

    source_fp.seekg(0, ios_base::end);
    long source_filesize = source_fp.tellg();

    cout << "source file size : " << source_filesize << endl;

     // 10MB 이하의 파일은 분할 할 필요가 없음
    if ( source_filesize <= _10MB )
    {
         cout << "No need to split!" << endl;
         source_fp.close();
         return false;
     }

     source_fp.seekg(ios_base::beg);

     if (!target_fp)
    {
        cout << "Error : Can't open target file !!" << endl;
        return false;
    }

    while(( !source_fp.eof() )&& (source_filesize > 0) )
    {
        memset(buffer, 0, 8192);

        if (source_filesize < read_size)
        {
            read_size = source_filesize;
         }

         source_fp.read( buffer, read_size );
         target_fp.write( buffer, read_size );
         count += read_size;
         source_filesize -= read_size;

        if (count >= _10MB)
        {
            count = 0;
            target_fp.close();
            cout << "Make " << nof_target+1 << " file!" <<endl;
            char next_target_filename[256];
            sprintf( next_target_filename, "%s.s%d", fileName, nof_target++ );
            target_fp.open(next_target_filename, ios::binary|ios::out|ios::trunc);
            if (!target_fp)
            {
                source_fp.close();
                cout << "Error : Can't open target file !!" << endl;
                return false;
             }
        }
    }

    source_fp.close();
    target_fp.close();

    make_batch_file (fileName, nof_target);

    return true;
}

void CSplit::make_batch_file (char *fileName, int count)
{
    int i = 0;

    ofstream target("run.bat", ios::out);
    target << "echo off" << endl;

    target << "if not exist "<< fileName <<".sh goto error" << endl;
    for (i = 0; i < count ; i++)
    {
         target << "if not exist " << fileName << ".s" << i << " goto error" << i << endl;
    }

    target << "copy /b "<< fileName <<".sh";
    for (i = 0; i < count ; i++)
    {
     target << " + " << fileName << ".s" << i;
    }
    target << " " << fileName << endl;
    target << "goto finish" << endl;

    target << ":error" << endl;
    target << "echo Error : " << fileName << ".sh not exist" << endl;
    target << "goto finish" << endl;

    for (i = 0; i < count; i++)
    {
        target << ":error" << i << endl;
        target << "echo Error : " << fileName <<".s" << i << " not exist" << endl;
        target << "goto finish" << endl;
    }
    target << ":finish" << endl;
    target << "echo Finish" <<endl;
    target.close();
}

int main(int argc, char **argv) {
    cout << "!!!Chobo Spilt V0.011!!!" << endl;

    CSplit mySplit;

    if (argc >= 2)
   {
       mySplit.split(argv[1]);
    }
    cout << "!!!End!!!" <<endl;
    return 0;
}



실행결과

생성파일 : Run.bat + 분할된 파일들

Run.bat 의 내용
echo off
if not exist test.zip.sh goto error
if not exist test.zip.s0 goto error0
if not exist test.zip.s1 goto error1
if not exist test.zip.s2 goto error2
if not exist test.zip.s3 goto error3
if not exist test.zip.s4 goto error4
copy /b test.zip.sh + test.zip.s0 + test.zip.s1 + test.zip.s2 + test.zip.s3 + test.zip.s4 test.zip
goto finish
:error
echo Error : test.zip.sh not exist
goto finish
:error0
echo Error : test.zip.s0 not exist
goto finish
:error1
echo Error : test.zip.s1 not exist
goto finish
:error2
echo Error : test.zip.s2 not exist
goto finish
:error3
echo Error : test.zip.s3 not exist
goto finish
:error4
echo Error : test.zip.s4 not exist
goto finish
:finish
echo Finish



'Coding > CPP 삽질기' 카테고리의 다른 글

간단한(?) 퀴즈  (2) 2011.02.06
[CPP] Lotto 생성기  (0) 2010.01.22
[CPP] 폴더내 파일 목록을 보여 주는 코드 조각  (1) 2009.03.24
Posted by chobocho