什么是“Jodatime”? /“阿帕奇公共空间” Zip/Unzip Java 实用程序?

发布于 2024-10-10 05:03:20 字数 707 浏览 12 评论 0原文

我确信有一个成熟的、广泛使用的 ZIP 文件实用程序,但我似乎找不到。与 Apache Commons、Google Collections、Joda Time 具有相同成熟度的东西

我正在尝试执行最简单的任务,即以字节数组 (ZipInputStream) 形式获取 zip 文件并将其提取到文件夹中。这似乎是一项非常乏味的任务。

我希望有一个语法糖 API 可以做这样的事情:

public class MyDreamZIPUtils 
      public static void extractToFolder(ZipInputStream zin, File outputFolderRoot){
           ...
      }
      public static void extractToFolder(ZipFile zf, File outputFolderRoot){
           ...
      }

      public static zipFolder(File folderToZip, File zippedFileLocation){
           ...
      }

      public static zipFolder(File folderToZip, ByteArrayOutputStream zipResult){
           ...
      }

有这样的事情吗? 我错过了什么吗?

I'm sure that there is a mature, widely used ZIP file utility out there, I just can't seem to find out. Something with the same maturity as Apache Commons, Google Collections, Joda Time

I'm trying to do the simplest task of getting a zip file as a byte array (ZipInputStream) and extract it to a folder. this seems like a very tedious task.

I would hope for a syntactic sugar API that does somethnig like this:

public class MyDreamZIPUtils 
      public static void extractToFolder(ZipInputStream zin, File outputFolderRoot){
           ...
      }
      public static void extractToFolder(ZipFile zf, File outputFolderRoot){
           ...
      }

      public static zipFolder(File folderToZip, File zippedFileLocation){
           ...
      }

      public static zipFolder(File folderToZip, ByteArrayOutputStream zipResult){
           ...
      }

Anything like this?
Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-10-17 05:03:20

http://commons.apache.org/compress/

我相信你可以写出“语法糖” ” 最重要的是。

Javadoc:http://commons.apache.org/compress/apidocs/index.html

http://commons.apache.org/compress/

I am sure you can write the "syntactic sugar" on top of that.

Javadoc: http://commons.apache.org/compress/apidocs/index.html

氛圍 2024-10-17 05:03:20

我只使用了Java API调用...我没有执行你所有的方法。你可以从这里弄清楚它们...请注意,我并不声称代码没有错误...使用风险自负:)

    public static void extractToFolder(ZipInputStream zin, File outputFolderRoot)
                throws IOException {

            FileOutputStream fos = null;
            byte[] buf = new byte[1024];
            ZipEntry zipentry;

            for (zipentry = zin.getNextEntry(); zipentry != null; zipentry = zin.getNextEntry()) {

                try {
                    String entryName = zipentry.getName();
                    System.out.println("Extracting: " + entryName);
                    int n;

                    File newFile = new File(outputFolderRoot, entryName);
                    if (zipentry.isDirectory()) {
                        newFile.mkdirs();
                        continue;
                    } else {
                        newFile.getParentFile().mkdirs();
                        newFile.createNewFile();
                    }

                    fos = new FileOutputStream(newFile);

                    while ((n = zin.read(buf, 0, 1024)) > -1)
                        fos.write(buf, 0, n);

                    fos.close();
                    zin.closeEntry();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null)
                        try {
                            fos.close();
                        } catch (Exception ignore) {
                        }
                }

            }

            zin.close();

        }


    public static void zipFolder(File folderToZip, File zippedFileLocation) throws IOException {
        // create a ZipOutputStream to zip the data to
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zippedFileLocation));
        String path = "";
        zipDir(folderToZip, zos, path);
        // close the stream
        zos.close();
    }

    private static void zipDir(File directory, ZipOutputStream zos, String path) throws IOException {
        File zipDir = directory;
        // get a listing of the directory content
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;
        // loop through dirList, and zip the files
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(zipDir, dirList[i]);
            if (f.isDirectory()) {
                zipDir(new File(f.getPath()), zos, path + f.getName() + "/");
                continue;
            }
            FileInputStream fis = new FileInputStream(f);
            try {
                ZipEntry anEntry = new ZipEntry(path + f.getName());
                zos.putNextEntry(anEntry);
                bytesIn = fis.read(readBuffer);
                while (bytesIn != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                    bytesIn = fis.read(readBuffer);
                }
            } finally {
                fis.close();
            }
        }
    }

参考 Java2s

I used only Java API calls... I did not do all your methods. you can figure them out from here... Please note i do not claim that the code is bug free... use at your own risk :)

    public static void extractToFolder(ZipInputStream zin, File outputFolderRoot)
                throws IOException {

            FileOutputStream fos = null;
            byte[] buf = new byte[1024];
            ZipEntry zipentry;

            for (zipentry = zin.getNextEntry(); zipentry != null; zipentry = zin.getNextEntry()) {

                try {
                    String entryName = zipentry.getName();
                    System.out.println("Extracting: " + entryName);
                    int n;

                    File newFile = new File(outputFolderRoot, entryName);
                    if (zipentry.isDirectory()) {
                        newFile.mkdirs();
                        continue;
                    } else {
                        newFile.getParentFile().mkdirs();
                        newFile.createNewFile();
                    }

                    fos = new FileOutputStream(newFile);

                    while ((n = zin.read(buf, 0, 1024)) > -1)
                        fos.write(buf, 0, n);

                    fos.close();
                    zin.closeEntry();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null)
                        try {
                            fos.close();
                        } catch (Exception ignore) {
                        }
                }

            }

            zin.close();

        }


    public static void zipFolder(File folderToZip, File zippedFileLocation) throws IOException {
        // create a ZipOutputStream to zip the data to
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zippedFileLocation));
        String path = "";
        zipDir(folderToZip, zos, path);
        // close the stream
        zos.close();
    }

    private static void zipDir(File directory, ZipOutputStream zos, String path) throws IOException {
        File zipDir = directory;
        // get a listing of the directory content
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[2156];
        int bytesIn = 0;
        // loop through dirList, and zip the files
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(zipDir, dirList[i]);
            if (f.isDirectory()) {
                zipDir(new File(f.getPath()), zos, path + f.getName() + "/");
                continue;
            }
            FileInputStream fis = new FileInputStream(f);
            try {
                ZipEntry anEntry = new ZipEntry(path + f.getName());
                zos.putNextEntry(anEntry);
                bytesIn = fis.read(readBuffer);
                while (bytesIn != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                    bytesIn = fis.read(readBuffer);
                }
            } finally {
                fis.close();
            }
        }
    }

Reference Java2s

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文