• Nebula3学习笔记(5): IO实战, ZIP解压缩程序


    上一次熟悉了IO系统后, 写个程序来练练手.

    正好这次看到App命名空间, 正好熟悉一下ConsoleApplication的用法. 因为Nebula3内置了ZipFileSystem, 但不支持压缩, 只支持解压缩, 就试着写了一个命令行的unzip.exe, 算是对之前所学的一个总结.

    没想解压缩就像拷贝文件一样简单! 因为当zip文件挂载到IO系统后, 可以像本地文件一样使用其中的文件, 呵呵.

     1: /********************************************************************
     2: 	created:	2008/07/08
     3: 	created:	8:7:2008   16:15
     4: 	filename: 	UnZip.cpp
     5: 	author:		xoyojank
     6: 	
     7: 	purpose:	zip file extract test
     8: *********************************************************************/
     9: 
    10: #include "stdneb.h"
    11: #include "UnZipApp.h"
    12: 
    13: using namespace Util;
    14: 
    15: //------------------------------------------------------------------------------
    16: /**
    17: */
    18: void __cdecl
    19: main(int argc, const char** argv)
    20: {
    21: 	CmdLineArgs args(argc, argv);
    22: 	UnZipApp app;
    23: 	app.SetCompanyName("Xoyojank");
    24: 	app.SetAppName("UnZip");
    25: 	app.SetCmdLineArgs(args);
    26: 	if (app.Open())
    27: 	{
    28: 		app.Run();
    29: 		app.Close();
    30: 	}
    31: 	system("pause");
    32: 	app.Exit();
    33: }
    
     1: /********************************************************************
     2: 	created:	2008/07/08
     3: 	created:	8:7:2008   16:16
     4: 	filename: 	UnZipApp.h
     5: 	author:		xoyojank
     6: 	
     7: 	purpose:	UnZip Application
     8: *********************************************************************/
     9: #pragma once
    10: #include "stdneb.h"
    11: #include "app/consoleapplication.h"
    12: 
    13: class UnZipApp : public App::ConsoleApplication
    14: {
    15: public:
    16: 	UnZipApp(void);
    17: 
    18: 	/// open the application
    19: 	virtual bool Open();
    20: 	/// run the application, return when user wants to exit
    21: 	virtual void Run();
    22: 
    23: private:
    24: 	/// a recursion method to unzip the files under "dir"
    25: 	void UnZipDir(Util::String& dir);
    26: private:
    27: 	Util::String zipFileName;
    28: 	Util::String sourcePath;
    29: 	Util::String targetPath;
    30: };
    
     1: /********************************************************************
     2: 	created:	2008/07/08
     3: 	created:	8:7:2008   16:19
     4: 	filename: 	UnZipApp.cpp
     5: 	author:		xoyojank
     6: 	
     7: 	purpose:	UnZip Application
     8: *********************************************************************/
     9: #include "UnZipApp.h"
    10: 
    11: 
    12: UnZipApp::UnZipApp(void)
    13: {
    14: }
    15: 
    16: bool UnZipApp::Open()
    17: {
    18: 	if (ConsoleApplication::Open())
    19: 	{
    20: 		// help info
    21: 		if (this->args.HasArg("-help"))
    22: 		{
    23: 			n_printf("-file: the .zip file to unzip.\n");
    24: 			n_printf("-path: where are the files unzip to, if this args is omitted, the file will be unzip into current directory.\n");
    25: 			return false;
    26: 		}
    27: 
    28: 		Util::String zipFile;
    29: 		zipFile = this->args.GetString("-file");
    30: 		// current .exe directory
    31: 		this->sourcePath = Util::String("bin:") + zipFile;
    32: 		bool fileValid = this->ioServer->MountZipArchive(this->sourcePath);
    33: 		if (!fileValid)
    34: 		{
    35: 			// absolute path
    36: 			this->sourcePath = Util::String("file:///") + zipFile;
    37: 			fileValid = this->ioServer->MountZipArchive(this->sourcePath);
    38: 			if (!fileValid)
    39: 			{
    40: 				n_error("Cannot open zip file.\n");
    41: 				return false;
    42: 			}
    43: 		}
    44: 		this->zipFileName = zipFile.ExtractFileName();
    45: 		this->zipFileName.StripFileExtension();
    46: 		this->sourcePath = this->sourcePath.ExtractDirName() + "/";
    47: 
    48: 		// target directory
    49: 		this->targetPath = this->args.GetString("-path");
    50: 		if (this->targetPath.Length() <= 1 || this->targetPath[1] != ':')
    51: 		{// relative path
    52: 			this->targetPath = Util::String("bin:") + this->targetPath;
    53: 		}
    54: 		else
    55: 		{// absolute path
    56: 			this->targetPath = Util::String("file:///") + this->targetPath;
    57: 		}
    58: 		this->targetPath += "/";
    59: 		if (this->sourcePath == this->targetPath)
    60: 		{
    61: 			n_printf("the source diretory cannot be the same with the destination!");
    62: 			return false;
    63: 		}
    64: 		return true;
    65: 	}
    66: 	return false;
    67: }
    68: 
    69: void UnZipApp::Run()
    70: {
    71: 	UnZipDir(this->zipFileName);
    72: }
    73: 
    74: void UnZipApp::UnZipDir( Util::String& dir )
    75: {
    76: 	// create a new directory
    77: 	this->ioServer->CreateDirectory(this->targetPath + dir);
    78: 	// unzip the files in this directory
    79: 	Util::Array<Util::String> listFile = this->ioServer->ListFiles(this->sourcePath + dir, "*");
    80: 	for (IndexT i = 0; i < listFile.Size(); i++)
    81: 	{
    82: 		Util::String curFile = this->targetPath + dir + "/" + listFile[i];
    83: 		this->ioServer->CopyFile(this->sourcePath + dir + "/" + listFile[i], curFile);
    84: 		n_printf("%s\n", curFile.AsCharPtr());
    85: 	}
    86: 	// unzip the sub directories
    87: 	Util::Array<Util::String> listDir = this->ioServer->ListDirectories(this->sourcePath + dir, "*");
    88: 	for (IndexT i = 0; i < listDir.Size(); i++)
    89: 	{
    90: 		Util::String curDir = dir + "/" + listDir[i];
    91: 		n_printf("%s\n", (this->targetPath + curDir).AsCharPtr());
    92: 		UnZipDir(curDir);
    93: 	}
    94: }
    

    调试参数:

    运行结果:

  • 相关阅读:
    一次完整的HTTP事务的过程
    移动端事件
    canvas 在视频中的用法
    理解Vue
    改变console.log的输出样式
    百度下拉搜索案例
    Vue总结
    字符串、正则
    .Net Mvc框架知识点
    Lowest Common Ancestor of a Binary Tree -- LeetCode
  • 原文地址:https://www.cnblogs.com/flying_bat/p/1354914.html
Copyright © 2020-2023  润新知