Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

Not able to open Zip files through cSharp which are created through java.util.zip

$
0
0

Hello Team,

 

We have simple java class which creates zip file, and we are able to open zip through windows unzip, 7-Zip etc... it just works fine. Also when we try using CSHARP we are getting

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

2014-12-08 12:26:51,152 WCProfilerService.BulkDownloadsScheduler:0 [9496] INFO  - Writing to log : Downloaded Confirms G07492038_20140916.zip as User 7690000030

2014-12-08 12:26:51,160 WCProfilerService.ZipUtil:0 [9496] DEBUG - G07492038_20140916/1.pdf

2014-12-08 12:26:51,165 WCProfilerService.BulkDownloadsScheduler:0 [9496] INFO  - Writing to log : Failed to Download: Wrong Local header signature: 0xA4ECFC79 Confirms G07492038_20140916.zip as User 7690000030

2014-12-08 12:26:51,165 WCProfilerService.BulkDownloadsScheduler:0 [9496] ERROR - Error in Downloading G07492038_20140916.zip

Exception: ICSharpCode.SharpZipLib.ZipException

Message: Wrong Local header signature: 0xA4ECFC79

Source: ICSharpCode.SharpZipLib

   at ICSharpCode.SharpZipLib.Zip.ZipInputStream.GetNextEntry()

   at WCProfilerService.ZipUtil.UnzipAllFiles(String zipFile, String destinationFolder)

   at WCProfilerService.DownloadJob.UnzipContents(String path)

   at WCProfilerService.BulkDownloadsScheduler.DownloadNextJob(Profile profile)

Here is the sample java code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;



public class AppZip {

    List<String> fileList;
    private static final String OUTPUT_ZIP_FILE = "TESTRunFile.zip";
    private static final String SOURCE_FOLDER = "C:/dev/BULK/G06197088_20141030";
 
    AppZip(){
    fileList = new ArrayList<String>();
    }
 
    public static void main( String[ args )
    {
        AppZip appZip = new AppZip();
        appZip.generateFileList(new File(SOURCE_FOLDER));
        appZip.zipIt(OUTPUT_ZIP_FILE);
    }
 
    /**
     * Zip it
     * @param zipFile output ZIP file location
     */
    public void zipIt(String zipFile){
 
     byte[ buffer = new byte[1024];
 
     try{
 
        FileOutputStream fos = new FileOutputStream(zipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
 
 
        System.out.println("Output to Zip : " + zipFile);
       
        final byte[ newLine = System.getProperty("line.separator").getBytes("UTF-8");
       
        for(String file : this.fileList){
 
            System.out.println("File Added : " + file);
            ZipEntry ze= new ZipEntry(file);
             
            //ze.setCrc(ZipOutputStream12.LOCSIG);
            zos.putNextEntry(ze);
 
            FileInputStream in =
                       new FileInputStream(file);
            BufferedReader bufRed = new BufferedReader(new FileReader(file));
 
            int len;
            String line;
            while ((line = bufRed.readLine()) != null) {
                final byte[ buff = line.getBytes("UTF-8");
                zos.write(buff, 0, buff.length);
                //zos.write(newLine, 0, newLine.length);
            }
 
            in.close();
        }
 
        zos.closeEntry();
        //remember close it
        zos.close();
 
        System.out.println("Done");
    }catch(IOException ex){
       ex.printStackTrace();  
    }
   }
 
    /**
     * Traverse a directory and get all files,
     * and add the file into fileList 
     * @param node file or directory
     */
    public void generateFileList(File node){
 
        //add file only
    if(node.isFile()){
        fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
    }
 
    if(node.isDirectory()){
        String[ subNote = node.list();
        for(String filename : subNote){
            generateFileList(new File(node, filename));
        }
    }
 
    }
 
    /**
     * Format the file path for zip
     * @param file file path
     * @return Formatted file path
     */
    private String generateZipEntry(String file){
        return file.substring(SOURCE_FOLDER.length()+1, file.length());
    }
}

 

 


Viewing all articles
Browse latest Browse all 1764

Trending Articles