wxWidgets教程(13)——wxArchive解压与压缩

一、wxArchive介绍

这是一个抽象类,无法实例化,需要继承它的子类。

wxArchiveInputStream:从压缩包中获取文档的输入数据流。

wxArchiveOutputStream:向压缩包中写入文档的输出数据流。

wxArchiveEntry:压缩包中文档的操作类。

二、wxZip使用说明

wxZipInputStream与wxZipOutputStream用来操作zip文件的。

1、读取文档操作的代码如下:

	wxFFileInputStream in(wxT("Desktop.zip"));
	wxZipInputStream zip(in);
	wxZipEntry myentry;

	for (wxZipEntry* entry; entry = zip.GetNextEntry();)
	{
		wxString filetype = entry->IsDir() ? wxT("目录") : wxT("文档");
		wxLogDebug(wxT("文件的类型:%s"), filetype);

		wxString texttype = entry->IsText() ? wxT("纯数据") : wxT("文件");
		wxLogDebug(wxT("文档的类型:%s"), texttype);

		wxString name = entry->GetName(wxPATH_WIN);
		wxLogDebug(wxT("文件名:%s"), name);

		wxString internalName = entry->GetInternalName();
		wxLogDebug(wxT("内部文件名:%s"), internalName);

		wxDateTime datetime = entry->GetDateTime();
		wxLogDebug(wxT("文件创建时间:%s"), datetime.FormatDate());

		wxInt32 method = entry->GetMethod();
		wxLogDebug(wxT("文件的压缩方式:%d"), method);

		wxInt32 model = entry->GetMode();
		wxLogDebug(wxT("文件的系统模式:%d"), model);

		wxFileOffset offset = entry->GetOffset();
		wxLogDebug(wxT("文件在压缩包中的偏移量:%lld"), offset);

		wxFileOffset offsetSize = entry->GetSize();
		wxLogDebug(wxT("文件的大小:%lld"), offsetSize);

		delete entry;
		entry = nullptr;
	}
	
	zip.CloseEntry();

2、写入文档的代码如下:

扫描二维码关注公众号,回复: 1697266 查看本文章

	wxFFileOutputStream out(wxT("test.zip"));
	wxZipOutputStream zip(out);
	wxTextOutputStream txt(zip);
	wxString sep = wxFileName::GetPathSeparator();

	zip.PutNextEntry(wxT("subdir") + sep + wxT("file1.txt"));
	txt << wxT("这里是一段文字");
	zip.PutNextEntry(wxT("file2.txt"));
	txt << wxT("这里是第二段文字");
	txt.Flush();

	zip.CloseEntry();
	zip.Close();

3、压缩文件与解压文件的例子:

压缩文件

	// 要被压缩的文件夹
	wxString path_dir_ziped = wxT("testdir");
	// 压缩目标
	wxFFileOutputStream out(wxT("test.zip"));
	wxZipOutputStream zip(out);
	// 中转的buffer流
	wxBufferedOutputStream bos(zip);
	// 获取所有的文件
	wxArrayString folders;
	wxDir::GetAllFiles(path_dir_ziped, &folders, wxEmptyString, wxDIR_DEFAULT);
	for (wxString filepath : folders) {
		// 读取文件内容
		wxFileInputStream fis(filepath);
		wxFileOffset len = fis.GetLength();
		char * buff = new char[len];
		fis.Read(buff, len);
		// 写入zip
		zip.PutNextEntry(filepath);
		bos.Write(buff, len);
		bos.Close();

		delete[] buff;
	}
	bos.Close();
	zip.CloseEntry();
	zip.Close();


解压文件

	// 压缩文件
	wxFFileInputStream in(wxT("test.zip"));
	wxZipInputStream zip(in);
	// 中转的buffer流
	wxBufferedInputStream bis(zip);
	
	for (wxZipEntry* entry; entry = zip.GetNextEntry();) {
		if (!entry->IsDir()) {
			wxString name = entry->GetName(wxPATH_WIN);
			// 判断文件路径是否存在,不存在就依次创建
			wxString parentDir = name.Mid(0, name.rfind(wxUChar('\\')));
			if (!wxDir::Exists(parentDir)) {
				wxDir::Make(parentDir, 511, wxPATH_MKDIR_FULL);
			}
			// 读取zip文件内容
			wxFileOffset len = entry->GetSize();
			char * buff = new char[len];
			bis.ReadAll(buff, len);
			// 写入到文件中
			wxFileOutputStream fos(name);
			fos.WriteAll(buff, len);
			fos.Close();
			delete[] buff;
			delete entry;
			entry = nullptr;
		}
	}
	zip.CloseEntry();

 三、wxTar与wxZlib使用说明

和wxZip一样的操作,只是换了个类名








猜你喜欢

转载自blog.csdn.net/wyansai/article/details/78486710