Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.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
Java Android压缩文件_Java_Android_File_Zip - Fatal编程技术网

Java Android压缩文件

Java Android压缩文件,java,android,file,zip,Java,Android,File,Zip,我已经尝试过搜索答案,但找不到任何答案,所以我正在发布。我正在尝试压缩文件,通过以下操作,我能够做到这一点。问题是,在本例中,文件名和文件路径是硬编码的。所以我试着根据需要修改代码 原始代码: // .... additional codes here String inputPath = Environment.getExternalStorageDirectory().getPath()+ "/TextFiles/"; String inputFile = "MyZip

我已经尝试过搜索答案,但找不到任何答案,所以我正在发布。我正在尝试压缩文件,通过以下操作,我能够做到这一点。问题是,在本例中,文件名和文件路径是硬编码的。所以我试着根据需要修改代码

原始代码:

   // .... additional codes here  
   String inputPath = Environment.getExternalStorageDirectory().getPath()+ "/TextFiles/";
   String inputFile = "MyZippedFiles.zip";


   ((Button) findViewById(R.id.button_zip))
   .setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                // declare an array for storing the files i.e the path of your source files
                s[0] = inputPath + "/01.txt";
                s[1] = inputPath + "/02.txt";

                /** 
                 * first parameter is the files, second parameter is zip file name 
                 * then call the zip function
                 */
                ZipManager zipManager = new ZipManager();
                zipManager.zip(s, inputPath + inputFile);
            }
        });
ZipManager.java:

public class ZipManager {
    private static final int BUFFER = 80000;

    public void zip(String[] _files, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < _files.length; i++) {
                Log.v("Compress", "Adding: " + _files[i]);
                FileInputStream fi = new FileInputStream(_files[i]);
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public class ZipManager {

    private static final int BUFFER = 80000;

    public void zip(String path, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < path.length(); i++) {
                Log.v("Compress", "Adding: " + path);
                FileInputStream fi = new FileInputStream(path);
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(path.substring(path.lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
ZipManager.java:

public class ZipManager {
    private static final int BUFFER = 80000;

    public void zip(String[] _files, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                    dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < _files.length; i++) {
                Log.v("Compress", "Adding: " + _files[i]);
                FileInputStream fi = new FileInputStream(_files[i]);
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public class ZipManager {

    private static final int BUFFER = 80000;

    public void zip(String path, String zipFileName) {
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
            byte data[] = new byte[BUFFER];

            for (int i = 0; i < path.length(); i++) {
                Log.v("Compress", "Adding: " + path);
                FileInputStream fi = new FileInputStream(path);
                origin = new BufferedInputStream(fi, BUFFER);

                ZipEntry entry = new ZipEntry(path.substring(path.lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;

                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }

            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
问题:


它正在创建压缩文件MyZippedFiles.zip,问题是,它不包含任何内容。文件大小为0。应该怎么做?TIA

让我给出一个简单的代码来创建所选文件的zip文件

@Override
public void onClick(View view) {

                File zipFile = new File(getFilesDir(), "zipImages.zip");

                try {

                    FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
                    zipFiles(imageFiles, fileOutputStream);
                    //Do whatever you want to do with zipFile
                     //for example
                     sendZipfile(zipFile);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
}
public void zipFiles(List<File> fileList, OutputStream os) throws IOException {

    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
    try {
        for (int i = 0; i < fileList.size(); ++i) {
            File file = fileList.get(i);
            String filename = file.getName();

            byte[] bytes = readFile(file);
            ZipEntry entry = new ZipEntry(filename);
            zos.putNextEntry(entry);
            zos.write(bytes);
            zos.closeEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        zos.close();
    }
}

public static byte[] readFile(File file) throws IOException {
    // Open file
    RandomAccessFile f = new RandomAccessFile(file, "r");
    try {
        // Get and check length
        long longlength = f.length();
        int length = (int) longlength;
        if (length != longlength)
            throw new IOException("File size >= 2 GB");
        // Read file and return data
        byte[] data = new byte[length];
        f.readFully(data);
        return data;
    } finally {
        f.close();
    }
}
@覆盖
公共void onClick(视图){
File zipFile=新文件(getFilesDir(),“zipImages.zip”);
试一试{
FileOutputStream FileOutputStream=新的FileOutputStream(zipFile);
ZipFile(图像文件、文件输出流);
//用zipFile做任何你想做的事
//比如说
sendZipfile(zipFile);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
}
public void zipFiles(列表文件列表、输出流os)引发IOException{
ZipOutputStream zos=新的zipoutpstream(新的缓冲输出流(os));
试一试{
对于(int i=0;i=2 GB”);
//读取文件并返回数据
字节[]数据=新字节[长度];
f、 准备好(数据);
返回数据;
}最后{
f、 close();
}
}

让我给出一个简单的代码,创建选定文件的zip文件

@Override
public void onClick(View view) {

                File zipFile = new File(getFilesDir(), "zipImages.zip");

                try {

                    FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
                    zipFiles(imageFiles, fileOutputStream);
                    //Do whatever you want to do with zipFile
                     //for example
                     sendZipfile(zipFile);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
}
public void zipFiles(List<File> fileList, OutputStream os) throws IOException {

    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(os));
    try {
        for (int i = 0; i < fileList.size(); ++i) {
            File file = fileList.get(i);
            String filename = file.getName();

            byte[] bytes = readFile(file);
            ZipEntry entry = new ZipEntry(filename);
            zos.putNextEntry(entry);
            zos.write(bytes);
            zos.closeEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        zos.close();
    }
}

public static byte[] readFile(File file) throws IOException {
    // Open file
    RandomAccessFile f = new RandomAccessFile(file, "r");
    try {
        // Get and check length
        long longlength = f.length();
        int length = (int) longlength;
        if (length != longlength)
            throw new IOException("File size >= 2 GB");
        // Read file and return data
        byte[] data = new byte[length];
        f.readFully(data);
        return data;
    } finally {
        f.close();
    }
}
@覆盖
公共void onClick(视图){
File zipFile=新文件(getFilesDir(),“zipImages.zip”);
试一试{
FileOutputStream FileOutputStream=新的FileOutputStream(zipFile);
ZipFile(图像文件、文件输出流);
//用zipFile做任何你想做的事
//比如说
sendZipfile(zipFile);
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
}
public void zipFiles(列表文件列表、输出流os)引发IOException{
ZipOutputStream zos=新的zipoutpstream(新的缓冲输出流(os));
试一试{
对于(int i=0;i=2 GB”);
//读取文件并返回数据
字节[]数据=新字节[长度];
f、 准备好(数据);
返回数据;
}最后{
f、 close();
}
}