【问题标题】:Android Knowing whether a File is a file(.jpg, etc) or a folder AND compressing a file(.png, etc) and a folderAndroid 知道文件是文件(.jpg 等)还是文件夹并压缩文件(.png 等)和文件夹
【发布时间】:2012-05-28 21:58:23
【问题描述】:

我目前正在创建一个使用 java.util.zip 包压缩文件的程序。它运作良好,但我注意到压缩文件夹/目录与压缩文件不同(如果我错了,请纠正我)。在我的程序中,我必须知道所选文件是文件夹还是文件(例如 .jpg、.png、.apk 等)。我已经尝试了一些实验让我知道,这是我的文件选择器活动中的示例代码: (currentDir 是一个文件)

    if(currentDir.isDirectory())Toast.makeText(this,"Directory",Toast.LENGTH_LONG).show();
    if(currentDir.isFile())Toast.makeText(this, "File", Toast.LENGTH_LONG).show();

插入后,我在活动中选择的每个文件都会输出一个“目录”而不是“文件”,即使我选择了一个图像。有人可以帮助我如何知道android中的文件是否是文件夹吗?谢谢! (第一个问题)

正如我之前所说,我注意到压缩文件夹与压缩文件不同(如果我错了,请纠正/教我)。在压缩文件夹时我已经完成了,但是在压缩文件(例如图像等)时,它仍然会压缩目录,但我相信必须只压缩我选择的文件。这是我的示例代码。 (第二题)

public void onClick(View v) {
    try{
        ZipOutputStream zos = new ZipOutputStream( new FileOutputStream(new File(currentDir.getPath()+".zip")) );
        zip( currentDir, currentDir, zos );
        zos.close();    
        Toast.makeText(this, "File successfully compressed!", Toast.LENGTH_LONG).show();
    }catch (IOException e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();}
}

private static final void zip(File directory, File base,ZipOutputStream zos) throws IOException {
    File[] files = directory.listFiles();
    byte[] buffer = new byte[8192];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
      if (files[i].isDirectory()) {
        zip(files[i], base, zos);
      } else {
        FileInputStream in = new FileInputStream(files[i]);
        ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
        zos.putNextEntry(entry);
        while (-1 != (read = in.read(buffer))) {
          zos.write(buffer, 0, read);
        }
        in.close();
      }
    }
  }

需要任何 cmets、帮助、建议、反应,我们将不胜感激!谢谢!

【问题讨论】:

    标签: android file compression zip directory


    【解决方案1】:
    public void createZipFile(String path) {
            File dir = new File(path);
            String[] list = dir.list();
            String name = path.substring(path.lastIndexOf("/"), path.length());
            String _path;
    
            if(!dir.canRead() || !dir.canWrite())
                return;
    
            int len = list.length;
    
            if(path.charAt(path.length() -1) != '/')
                _path = path + "/";
            else
                _path = path;
    
            try {
                ZipOutputStream zip_out = new ZipOutputStream(
                                          new BufferedOutputStream(
                                          new FileOutputStream(_path + name + ".zip"), BUFFER));
    
                for (int i = 0; i < len; i++)
                    zip_folder(new File(_path + list[i]), zip_out);
    
                zip_out.close();
    
            } catch (FileNotFoundException e) {
                Log.e("File not found", e.getMessage());
    
            } catch (IOException e) {
                Log.e("IOException", e.getMessage());
            }
        }
    

    试试这个代码。

    【讨论】:

    • 感谢您的建议!我目前正在尝试这段代码。但我不知道你是如何声明 'BUFFER' 的,并且缺少方法 'zip_folder'。你也会提供吗?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 2012-01-14
    • 2010-12-14
    • 2012-07-13
    • 2023-03-18
    • 2015-04-05
    • 2014-06-14
    相关资源
    最近更新 更多