Friday, August 13, 2010

using java.util.zip for Zipping the files

 The follwoing code snippet is to Zip multiple files into a single file using java.util.zip .


private void ZipFiles()
        {
           OpenFileDialog fd1 = new OpenFileDialog();
            fd1.Multiselect = true;
            
            if (fd1.ShowDialog() == DialogResult.OK)
            {
               SaveFileDialog sf1 = new SaveFileDialog();
               sf1.Filter = "ZIP Files|*.zip";
                //sf1.Filter = "RAR Files|*.rar";
                if (sf1.ShowDialog() == DialogResult.OK)
                {
                    
                    string zipFileName = sf1.FileName.ToString();
                    ZipOutputStream os = new ZipOutputStream(new java.io.FileOutputStream(zipFileName));
                    java.io.FileInputStream fs;

                    for (int i = 0; i < fd1.FileNames.Length; i++)
                    {
                        string[] arr = fd1.FileNames;

                        string sourceFileName = arr.ToString();
                       
                        string path = Path.GetFileNameWithoutExtension(sourceFileName);
                        string path2 = Path.GetFullPath(sourceFileName);
                        string path1 = Path.GetExtension(path2);
                        if (path1 == ".pdf")
                        {
                            ZipEntry ze = new ZipEntry(path + ".pdf");
                            ze.setMethod(ZipEntry.DEFLATED);
                            os.putNextEntry(ze);
                            //java.io.FileInputStream fs = new java.io.FileInputStream(str);
                            fs = new java.io.FileInputStream(sourceFileName);

                            sbyte[] buff = new sbyte[1024];
                            int n = 0;
                            while ((n = fs.read(buff, 0, buff.Length)) > 0)
                            {
                                os.write(buff, 0, n);

                            }
                            fs.close();
                        }
                        if (path1 == ".txt")
                        {
                            ZipEntry ze = new ZipEntry(path + ".txt");
                            ze.setMethod(ZipEntry.DEFLATED);
                            os.putNextEntry(ze);
                            //java.io.FileInputStream fs = new java.io.FileInputStream(str);
                            fs = new java.io.FileInputStream(sourceFileName);

                            sbyte[] buff = new sbyte[1024];
                            int n = 0;
                            while ((n = fs.read(buff, 0, buff.Length)) > 0)
                            {
                                os.write(buff, 0, n);

                            }
                            fs.close();
                        }
                        if (path1 == ".doc")
                        {
                            ZipEntry ze = new ZipEntry(path + ".doc");
                            ze.setMethod(ZipEntry.DEFLATED);
                            os.putNextEntry(ze);
                            //java.io.FileInputStream fs = new java.io.FileInputStream(str);
                            fs = new java.io.FileInputStream(sourceFileName);

                            sbyte[] buff = new sbyte[1024];
                            int n = 0;
                            while ((n = fs.read(buff, 0, buff.Length)) > 0)
                            {
                                os.write(buff, 0, n);

                            }
                            fs.close();
                        }
                        if (path1 == ".xls")
                        {
                            ZipEntry ze = new ZipEntry(path + ".xls");
                            ze.setMethod(ZipEntry.DEFLATED);
                            os.putNextEntry(ze);
                            //java.io.FileInputStream fs = new java.io.FileInputStream(str);
                            fs = new java.io.FileInputStream(sourceFileName);

                            sbyte[] buff = new sbyte[1024];
                            int n = 0;
                            while ((n = fs.read(buff, 0, buff.Length)) > 0)
                            {
                                os.write(buff, 0, n);

                            }
                            fs.close();
                        }
                    }

                    //fs.close();
                    os.closeEntry();
                    os.close();
                    // }

                    MessageBox.Show("Compression ok!");
                }
            }
            else
            {

            }

        }

0 comments:

Post a Comment