在Windows中创建Zip文件并在Linux中提取Zip文件 [英] Create Zip File In Windows and Extract Zip File In Linux

查看:136
本文介绍了在Windows中创建Zip文件并在Linux中提取Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows下创建了一个zip文件(连同目录),如下所示(代码选自 http://www.exampledepot.com/egs/java.util.zip/CreateZip.html ):

I had created a zip file (together with directory) under Windows as follow (Code are picked from http://www.exampledepot.com/egs/java.util.zip/CreateZip.html) :

package sandbox;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author yan-cheng.cheok
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // These are the files to include in the ZIP file
        String[] filenames = new String[]{"MyDirectory" + File.separator + "MyFile.txt"};

        // Create a buffer for reading the files
        byte[] buf = new byte[1024];

        try {
            // Create the ZIP file
            String outFilename = "outfile.zip";
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

            // Compress the files
            for (int i=0; i<filenames.length; i++) {
                FileInputStream in = new FileInputStream(filenames[i]);

                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(filenames[i]));

                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();
            }

            // Complete the ZIP file
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

使用在Windows下可以毫无问题地提取新创建的zip文件http://www.exampledepot.com/egs/java.util.zip/GetZip.html

然而,我意识到如果我提取新创建的Linux下的zip文件,使用 http://www.exampledepot的修改版本。 COM / EGS / java.util.zip / GetZip.html 。原始版本不使用zipEntry.isDirectory()检查目录。

However, I realize if I extract the newly created zip file under Linux, using modified version of http://www.exampledepot.com/egs/java.util.zip/GetZip.html. The original version doesn't check for directory using zipEntry.isDirectory()).

public static boolean extractZipFile(File zipFilePath, boolean overwrite) {
    InputStream inputStream = null;
    ZipInputStream zipInputStream = null;
    boolean status = true;

    try {
        inputStream = new FileInputStream(zipFilePath);

        zipInputStream = new ZipInputStream(inputStream);
        final byte[] data = new byte[1024];

        while (true) {
            ZipEntry zipEntry = null;
            FileOutputStream outputStream = null;

            try {
                zipEntry = zipInputStream.getNextEntry();

                if (zipEntry == null) break;

                final String destination = Utils.getUserDataDirectory() + zipEntry.getName();

                if (overwrite == false) {
                    if (Utils.isFileOrDirectoryExist(destination)) continue;
                }

                if (zipEntry.isDirectory())
                {
                    Utils.createCompleteDirectoryHierarchyIfDoesNotExist(destination);
                }
                else
                {
                    final File file = new File(destination);
                    // Ensure directory is there before we write the file.
                    Utils.createCompleteDirectoryHierarchyIfDoesNotExist(file.getParentFile());

                    int size = zipInputStream.read(data);

                    if (size > 0) {
                        outputStream = new FileOutputStream(destination);

                        do {
                            outputStream.write(data, 0, size);
                            size = zipInputStream.read(data);
                        } while(size >= 0);
                    }
                }
            }
            catch (IOException exp) {
                log.error(null, exp);
                status = false;
                break;
            }
            finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    }
                    catch (IOException exp) {
                        log.error(null, exp);
                        break;
                    }
                }

                if (zipInputStream != null) {
                    try {
                        zipInputStream.closeEntry();
                    }
                    catch (IOException exp) {
                        log.error(null, exp);
                        break;
                    }
                }
            }

        }   // while(true)
    }
    catch (IOException exp) {
        log.error(null, exp);
        status = false;
    }
    finally {
        if (zipInputStream != null) {
            try {
                zipInputStream.close();
            } catch (IOException ex) {
                log.error(null, ex);
            }
        }

        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException ex) {
                log.error(null, ex);
            }
        }
    }
    return status;
}

MyDirectory \ MyFile.txt而不是MyFile.txt放在MyDirectory文件夹下。

我尝试通过将zip文件创建代码更改为

I try to solve the problem by changing the zip file creation code to

    String[] filenames = new String[]{"MyDirectory" + "/" + "MyFile.txt"};

但是,这是一个合格的解决方案,通过硬编码分隔符?它会在Mac OS下运行吗? (我没有Mac试用)

But, is this an eligible solution, by hard-coded the seperator? Will it work under Mac OS? (I do not have a Mac to try out)

推荐答案

是的,你的解决方案(虽然显然不优雅)是正确的方法。
/应该在zipentry中使用,而不是本地File.separator

Yes, your solution (though apparently inelegant) is the right way. The "/" should be used inside zipentry, not the local File.separator

这篇关于在Windows中创建Zip文件并在Linux中提取Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆