English 中文(简体)
了解文件是文件(.jpg, etc) 还是文件夹, 压缩文件(. png, etc) 和文件夹
原标题:Android Knowing whether a File is a file(.jpg, etc) or a folder AND compressing a file(.png, etc) and a folder

I m currently creating a program that compresses a file by using java.util.zip package. It works well but I noticed that compressing a folder/directory is different from compressing a file (correct me if I m wrong). In my program, I have to know whether a selected file is a folder or a file(such as .jpg, .png, .apk and etc). I ve tried some experiments for me to be able to know and here is the sample code in my filechooser activity : (currentDir is a File)

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

在此插入之后, 在我的活动中选择的每一个文件都会输出一个“ 日志” 而不是一个“ 文件 ”, 即使我选择了一个图像 。 有人能帮助我了解一个文件是否是一个文件夹吗? 谢谢! (第一个问题是)

正如我先前说过的,我注意到压缩一个文件夹与压缩一个文件不同(更正/如果我错了,就教给我)。关于压缩一个文件夹,我已经做了。但在压缩一个文件,如图像等,它仍然压缩目录,但我相信,只压缩我所选择的文件。这是我的样本代码。 (第二个问题)

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();
      }
    }
  }

任何评论、帮助、建议、反应都是需要的,

问题回答
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());
        }
    }

用这个代码尝试 。





相关问题
BlackBerry - Unpack Zip File

I m developing a BlackBerry application in which I need to unpack a zip file compressed with PKZIP. The package could have one file in it, or it could have 10; it will vary in each case. I know that ...

How to find if a request is for js or css in httpHandler

is there any way to find if a particular request is for JS or CSS in httphandler to improve the performance of my website i was using HttpCompress from Code Project http://www.codeproject.com/KB/...

Compress data before storage on Google App Engine

I im trying to store 30 second user mp3 recordings as Blobs in my app engine data store. However, in order to enable this feature (App Engine has a 1MB limit per upload) and to keep the costs down I ...

How to process compressed data in Java

I have some data which takes up more than 50MB in an uncompressed file, but compresses down to less than half a MB using gzip. Most of this is numerical data. I m trying to figure out how to process ...

What is the best way to extract a zip file using java

I have a a zipped file. That file contains various directories and files also. I want to extract all those and save in a specified path. So How to write a java program to extract the zipped file. ...

热门标签