Java Basics - 循环文件夹

时间:2018-02-13 19:14:30

标签: java loops

我是java的新手,来自js背景。我试图遍历一个文件夹并压缩它。目前,我已成功完成了压缩部分,但通过静态添加文件来完成。答案显然是从编程角度来看的循环。我无法循环列表并使其等于下面的压缩方法。由于我的初学者技能,在线资源对我来说没有多大意义。

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

                public class ZipFiles {

                    public static void main(String[] args) {

                        try {
                            FileOutputStream fos = new FileOutputStream("atest.zip");
                            ZipOutputStream zos = new ZipOutputStream(fos);

                            String file1Name = "src/resources/text1";
                            String file2Name = "src/resources/text2";
                            String file3Name = "src/resources/text3";
                            String file4Name = "src/resources/text4";
                            String file5Name = "src/resources/text5";
                            String file6Name = "src/resources/text6";



                            addToZipFile(file1Name, zos);
                            addToZipFile(file2Name, zos);
                            addToZipFile(file3Name, zos);
                            addToZipFile(file4Name, zos);
                            addToZipFile(file5Name, zos);
                            addToZipFile(file6Name, zos);


                            zos.close();
                            fos.close();

                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }


                    public static void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {

                        System.out.println("Writing '" + fileName + "' to zip file");

                        File file = new File(fileName);
                        FileInputStream fis = new FileInputStream(file);
                        ZipEntry zipEntry = new ZipEntry(fileName);
                        zos.putNextEntry(zipEntry);

                        byte[] bytes = new byte[1024];
                        int length;
                        while ((length = fis.read(bytes)) >= 0) {
                            zos.write(bytes, 0, length);
                        }

                        zos.closeEntry();
                        fis.close();
                    }

                }

2 个答案:

答案 0 :(得分:1)

答案在本文中:http://www.baeldung.com/java-compress-and-uncompress

此代码会压缩多个文件(与您的代码非常相似但略有变化):

gdist

修改 代码中的这一行创建了一个在gdist(nests$lat, nests$lon, foray$lat, foray$lon, units="m", verbose=FALSE) Error in while (abs(lamda - lamda.old) > 1e-11) { : missing value where TRUE/FALSE needed In addition: Warning messages: 1: In Ops.factor(lon.1, rad) : ‘*’ not meaningful for factors 2: In Ops.factor(lat.1, rad) : ‘*’ not meaningful for factors 3: In Ops.factor(lon.2, rad) : ‘*’ not meaningful for factors 4: In Ops.factor(lat.2, rad) : ‘*’ not meaningful for factors 5: In lon.1 - lon.2 : longer object length is not a multiple of shorter object length 6: In while (abs(lamda - lamda.old) > 1e-11) { : the condition has length > 1 and only the first element will be used 循环中很容易通过的数组: public class ZipMultipleFiles { public static void main(String[] args) throws IOException { List<String> srcFiles = Arrays.asList("test1.txt", "test2.txt"); FileOutputStream fos = new FileOutputStream("multiCompressed.zip"); ZipOutputStream zipOut = new ZipOutputStream(fos); for (String srcFile : srcFiles) { File fileToZip = new File(srcFile); FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } fis.close(); } zipOut.close(); fos.close(); } }

答案 1 :(得分:0)

由于Elliotk链接,基本上用于查找文件夹方法的子项。我正在使字符串等于父文件夹的路径 - &gt;检查它是否是一个目录 - &gt;列出其文件 - &gt;获取名称和while循环将所有这些写入压缩文件夹

这是我的整个代码

    package zipfolder2;

    import java.io.*;
    import java.util.*;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;

    public class zipfolders2 {

        public static void main(String[] args) {
            try {

                String sourceFile = "src/resources";
                FileOutputStream fos = new FileOutputStream("zippedfiles.zip");
                ZipOutputStream zipOut = new ZipOutputStream(fos);
                File fileToZip = new File(sourceFile);

                zipFile(fileToZip, fileToZip.getName(), zipOut);
                zipOut.close();
                fos.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
            if (fileToZip.isHidden()) {
                return;
            }
            if (fileToZip.isDirectory()) {
                File[] children = fileToZip.listFiles();
                for (File childFile : children) {
                    zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
                }
                return;
            }
            FileInputStream fis = new FileInputStream(fileToZip);
            ZipEntry zipEntry = new ZipEntry(fileName);
            zipOut.putNextEntry(zipEntry);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = fis.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }
            fis.close();
        }

    }