Home Java Basics - Looping through folder
Reply: 2

Java Basics - Looping through folder

Anu Rajan
1#
Anu Rajan Published in 2018-02-13 19:14:30Z

I am very new to java and coming from a js background. I am attempting to loop through a folder full of files and zipping it. Currently, I have done the zipping part successfully, but doing by statically adding the files. The answer is obviously a loop from a programming perspective. I am having trouble looping a list and making it equal to the zipping method below. Online Resources are not making sense much sense to me due to my beginner skill.

                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();
                    }

                }
ElliotK
2#
ElliotK Reply to 2018-02-13 19:20:24Z

The answer is in this article: http://www.baeldung.com/java-compress-and-uncompress

This code zips multiple files (Very similar to your code but slightly changed):

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();
    }
}

EDIT: This line in the code creates an array that is easy to go through in a while loop: List<String> srcFiles = Arrays.asList("test1.txt", "test2.txt");

Anu Rajan
3#
Anu Rajan Reply to 2018-02-13 20:47:54Z

basically used finding children of a folder method thanks to Elliotk link. I am making the string equal to the path of the parent folder - >checking if whether if its a directory - > list its files -> get the names and while loop to write all of them to a zipped folder

here is my whole code

    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();
        }

    }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331831 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO