解压文件 Unzip

 package com.scnu.zip;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
 
/**
 * UnZip -- print or unzip a JAR or PKZIP file using java.util.zip.

Command-line
 * version: extracts files.
 *
 * @author Ian Darwin, Ian@DarwinSys.com $Id: UnZip.java,v 1.7 2004/03/07
 *         17:40:35 ian Exp $
 */
public class UnZip {
  /** Constants for mode listing or mode extracting. */
  public static final int LIST = 0, EXTRACT = 1;
 
  /** Whether we are extracting or just printing TOC */
  protected int mode = LIST;
 
  /** The ZipFile that is used to read an archive */
  protected ZipFile zippy;
 
  /** The buffer for reading/writing the ZipFile data */
  protected byte[] b;
 
  /**
   * Simple main program, construct an UnZipper, process each .ZIP file

from
   * argv[] through that object.
   */
    //java Unzip zipfile.zip
  public static void main(String[] argv) {
    UnZip u = new UnZip();
 
    for (int i = 0; i < argv.length; i++) {
      if ("-x".equals(argv[i])) {
        u.setMode(EXTRACT);
        continue;
      }
      String candidate = argv[i];
      // System.err.println("Trying path " + candidate);
      if (candidate.endsWith(".zip") || candidate.endsWith(".jar"))
        u.unZip(candidate);
      else
        System.err.println("Not a zip file? " + candidate);
    }
    System.err.println("All done!");
  }
 
  /** Construct an UnZip object. Just allocate the buffer */
  UnZip() {
    b = new byte[8092];
  }
 
  /** Set the Mode (list, extract). */
  protected void setMode(int m) {
    if (m == LIST || m == EXTRACT)
      mode = m;
  }
 
  /** Cache of paths we've mkdir()ed. */
  protected SortedSet dirsMade;
 
  /** For a given Zip file, process each entry. */
  public void unZip(String fileName) {
    dirsMade = new TreeSet();
    try {
      zippy = new ZipFile(fileName);
      Enumeration all = zippy.entries();
      while (all.hasMoreElements()) {
        getFile((ZipEntry) all.nextElement());
      }
    } catch (IOException err) {
      System.err.println("IO Error: " + err);
      return;
    }
  }
 
  protected boolean warnedMkDir = false;
 
  /**
   * Process one file from the zip, given its name. Either print the

name, or
   * create the file on disk.
   */
  protected void getFile(ZipEntry e) throws IOException {
    String zipName = e.getName();
    switch (mode) {
    case EXTRACT:
      if (zipName.startsWith("/")) {
        if (!warnedMkDir)
          System.out.println("Ignoring absolute paths");
        warnedMkDir = true;
        zipName = zipName.substring(1);
      }
      // if a directory, just return. We mkdir for every file,
      // since some widely-used Zip creators don't put out
      // any directory entries, or put them in the wrong place.
      if (zipName.endsWith("/")) {
        return;
      }
      // Else must be a file; open the file for output
      // Get the directory part.
      int ix = zipName.lastIndexOf('/');
      if (ix > 0) {
        String dirName = zipName.substring(0, ix);
        if (!dirsMade.contains(dirName)) {
          File d = new File(dirName);
          // If it already exists as a dir, don't do anything
          if (!(d.exists() && d.isDirectory())) {
            // Try to create the directory, warn if it fails
            System.out.println("Creating Directory: " + dirName);
            if (!d.mkdirs()) {
              System.err.println("Warning: unable to mkdir "
                  + dirName);
            }
            dirsMade.add(dirName);
          }
        }
      }
      System.err.println("Creating " + zipName);
      FileOutputStream os = new FileOutputStream(zipName);
      InputStream is = zippy.getInputStream(e);
      int n = 0;
      while ((n = is.read(b)) > 0)
        os.write(b, 0, n);
      is.close();
      os.close();
      break;
    case LIST:
      // Not extracting, just list
      if (e.isDirectory()) {
        System.out.println("Directory " + zipName);
      } else {
        System.out.println("File " + zipName);
      }
      break;
    default:
      throw new IllegalStateException("mode value (" + mode + ") bad");
    }
  }
}
运行: java UnZip -x zipfile.zip
### 回答1: MFC(Microsoft Foundation Class)是一种用于开发Windows应用程序的框架,其中包含了丰富的类库和基础设施,用于简化Windows开发过程。在MFC中,可以使用ZipArchive类来解压文件。 压缩文件unzip.cpp,指的是实现解压文件功能的源代码文件。 在unzip.cpp中,我们可以使用MFC提供的ZipArchive类来进行解压缩操作。首先,我们需要包含相应的头文件: #include <afx.h> #include <afxwin.h> #include <afxext.h> 然后,我们需要创建一个ZipArchive对象,并指定待解压缩的文件路径: CZipArchive zip; CString filePath = "待解压文件的路径"; zip.Open(filePath); 接下来,我们可以使用zip.GetFileCount()方法获取压缩文件中的文件数量,并通过循环遍历每个文件: int fileCount = zip.GetFileCount(); for (int i = 0; i < fileCount; i++) { CZipFile *zipFile = zip.GetFileInfo(i); // 获取文件信息 CString fileName = zipFile->GetFileName(); // 获取文件名 CString targetPath = "目标解压路径" + fileName; // 设置解压后的文件路径 zipFile->Open(); // 打开文件 FILE *fp = fopen(targetPath, "wb"); // 创建目标文件 char buffer[1024]; UINT bytesRead; while ((bytesRead = zipFile->Read(buffer, 1024)) > 0) { fwrite(buffer, bytesRead, 1, fp); // 逐块写入目标文件 } fclose(fp); // 关闭文件 zipFile->Close(); // 关闭ZIP文件 } 最后,记得关闭ZipArchive对象: zip.Close(); 以上就是使用MFC解压文件的简要过程。通过这段代码,我们可以实现对压缩文件解压缩操作。当然,具体的实现细节还需要根据实际需求进行调整和完善。 ### 回答2: MFC压缩文件解压缩主要通过使用CFile类和CArchive类来实现。以下是一个使用MFC解压文件的示例代码(unzip.cpp): #include "stdafx.h" #include "unzip.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // 函数头声明 void UnzipFile(const CString& strZipFile, const CString& strDestFolder); // 执行解压缩的函数 void UnzipFile(const CString& strZipFile, const CString& strDestFolder) { // 创建解压文件的CFile对象 CFile file; file.Open(strZipFile, CFile::modeRead); // 创建CArchive对象来实现解压缩 CArchive ar(&file, CArchive::load); // 获取目标文件夹的路径并确保其存在 CString strFolder = strDestFolder + "\\"; if (!PathFileExists(strFolder)) { CreateDirectory(strFolder, NULL); } // 循环解压文件 while (!ar.IsBufferEmpty()) { // 在目标文件夹中创建一个新文件 CString strFileName; ar >> strFileName; strFileName = strFolder + strFileName; CFile newFile; newFile.Open(strFileName, CFile::modeCreate | CFile::modeWrite); // 将数据从归档对象写入新文件 UINT nLength = ar.GetFile()->GetLength(); BYTE* pBuf = new BYTE[nLength]; ar.Read(pBuf, nLength); newFile.Write(pBuf, nLength); // 写入完成后关闭新文件 newFile.Close(); } // 关闭解压缩归档对象 ar.Close(); file.Close(); } 使用上述代码,可以将压缩文件解压缩到指定的目标文件夹中。解压缩功能通过使用CFile类和CArchive类实现,循环将解压缩的文件从归档对象中读取并写入到目标文件夹中的新文件中。解压缩前需要确保目标文件夹存在,并在解压缩完成后关闭文件和归档对象。 注意:上述代码仅供参考,实际使用时,可能需要根据具体的需求进行修改和适配。 ### 回答3: MFC是Microsoft Foundation Classes的缩写,它是微软公司提供的一套面向对象的C++类库,用于开发Windows应用程序。unzip.cpp是用于解压文件的代码文件。 在MFC中,要实现文件解压缩功能,可以利用CFile类和CFileException类来操作文件。 首先,需要声明一个CFile对象,并打开要解压缩的文件。可以使用CFile::Open()函数打开文件,并在打开文件时检查是否正常打开。 然后,可以利用CFile类的Read()函数读取文件中的内容,并且将读取的内容写入到目标文件中。要解压文件,需要读取压缩文件中的每个字节,并根据压缩格式的规则进行解压缩。 在解压缩过程中,可以利用缓冲区来提高读写的效率。可以使用BYTE类型的数组作为缓冲区,使用Read()函数从文件中读取数据,然后使用Write()函数将数据写入到目标文件中。 解压缩完成后,应该关闭文件。可以使用CFile的Close()函数来关闭文件。 以上就是使用MFC的CFile类实现文件解压缩的大致步骤。当然,具体还需要根据解压文件的格式和要求进行相应的编码和操作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值