Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android压缩文件夹_Android - Fatal编程技术网

android压缩文件夹

android压缩文件夹,android,Android,我正在做一个简单的android应用程序,其中包括压缩文件夹。实际上,压缩过程已经完成,文件保存在定义的位置。但问题是,当我从模拟器中推送压缩文件并手动解压缩时,文件已损坏。有什么问题吗。当我们手动解压时,文件是否已损坏 我的文件夹结构如下所示 String zipFile = "/mnt/sdcard/Testplay1.zip"; byte[] buffer = new byte[1024]; FileOutputStream fos = null; try { fos = new

我正在做一个简单的android应用程序,其中包括压缩文件夹。实际上,压缩过程已经完成,文件保存在定义的位置。但问题是,当我从模拟器中推送压缩文件并手动解压缩时,文件已损坏。有什么问题吗。当我们手动解压时,文件是否已损坏

我的文件夹结构如下所示

String zipFile = "/mnt/sdcard/Testplay1.zip";
byte[] buffer = new byte[1024];
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(zipFile);
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
}
ZipOutputStream zos = new ZipOutputStream(fos);
updata = new ArrayList<File>();
contentpath = new File(FOLDER_PATH);
try {
    if (contentpath.isDirectory()) {
        File[] listFile = contentpath.listFiles();
        for (int i = 0; i < listFile.length; i++) {
            if (listFile[i].isDirectory()) {
                File[] contfile = listFile[i].listFiles();
                for (int j = 0; j < contfile.length; j++) {
                    updata.add(contfile[j]);
                    FileInputStream fis = new FileInputStream(contfile[j]);
                    zos.putNextEntry(new ZipEntry(contfile[j].getName()));
                    int length;
                    while ((length = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, length);
                    }
                    zos.closeEntry();
                    // close the InputStream
                    fis.close();
                }
            }
        }
        zos.close();
    }
    System.out.println("Testplay1 Folder contains=>" + updata);
} catch (Exception e) {
    e.printStackTrace();
    // TODO: handle exception
}
TestPlay1-包含两个子目录-播放列表和内容
目录播放列表包含一个xml文件
目录内容包含图像和视频等文件

我的代码如下

String zipFile = "/mnt/sdcard/Testplay1.zip";
byte[] buffer = new byte[1024];
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(zipFile);
} catch (FileNotFoundException e1) {
    e1.printStackTrace();
}
ZipOutputStream zos = new ZipOutputStream(fos);
updata = new ArrayList<File>();
contentpath = new File(FOLDER_PATH);
try {
    if (contentpath.isDirectory()) {
        File[] listFile = contentpath.listFiles();
        for (int i = 0; i < listFile.length; i++) {
            if (listFile[i].isDirectory()) {
                File[] contfile = listFile[i].listFiles();
                for (int j = 0; j < contfile.length; j++) {
                    updata.add(contfile[j]);
                    FileInputStream fis = new FileInputStream(contfile[j]);
                    zos.putNextEntry(new ZipEntry(contfile[j].getName()));
                    int length;
                    while ((length = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, length);
                    }
                    zos.closeEntry();
                    // close the InputStream
                    fis.close();
                }
            }
        }
        zos.close();
    }
    System.out.println("Testplay1 Folder contains=>" + updata);
} catch (Exception e) {
    e.printStackTrace();
    // TODO: handle exception
}
String zipFile=“/mnt/sdcard/Testplay1.zip”;
字节[]缓冲区=新字节[1024];
FileOutputStream=null;
试一试{
fos=新文件输出流(zipFile);
}捕获(FileNotFoundException e1){
e1.printStackTrace();
}
ZipoutStream zos=新ZipoutStream(fos);
updatea=newarraylist();
contentpath=新文件(文件夹路径);
试一试{
if(contentpath.isDirectory()){
File[]listFile=contentpath.listFiles();
for(int i=0;i0){
写入(缓冲区,0,长度);
}
zos.closeEntry();
//关闭输入流
fis.close();
}
}
}
zos.close();
}
System.out.println(“Testplay1文件夹包含=>”+updatea);
}捕获(例外e){
e、 printStackTrace();
//TODO:处理异常
}

我建议您使用Zip4j:
在那里,您只需将文件夹压缩,其余工作由库完成

ZipFile zipfile = new ZipFile("/mnt/sdcard/bla.zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipfile.addFolder("/mnt/sdcard/folderToZip", parameters);

谢谢你的答复。。你能告诉我它的用法吗。我下载了eclipse工作空间的zip文件。那么如何处理这个zip文件呢?请帮我解开它,并把JAR文件复制到你的Android项目的“LIBS”文件夹中,你应该考虑使用Apache压缩库。无需重新发明任何这些常见操作。。