title: Java code for building epub is not working
body: <p>I made a java class to read a txt file and convert into EPUB format, My code is able to generated necessary files into ZipOutputStream (java.util.zip) object.
  Generated zio file contains following files:</p>
  
  <ol>
  <li>index.html </li>
  <li>mimetype </li>
  <li>content.opf </li>
  <li>toc.ncx </li>
  <li>META-INF/container.xml</li>
  </ol>
  
  <p>But when I tried to load epub file is it unworkable. Online epub validation (http://threepress.org/document/epub-validate/) throws error that "META-INF/container.xml" file was not found, however strange thing about this is I can extract zip archive properly and everything is in place also if I recreate extract to a zip file using windows winzip utility that epub is working!
  Here is the piece of code used to create container.xml file</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.util.Enumeration;
  import java.util.Iterator;
  import java.util.zip.Deflater;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipFile;
  import java.util.zip.ZipOutputStream;
  
  import javax.swing.JPopupMenu.Separator;
  
  public class ToEPub {
  
  private String rootDir;
  
  private static String metaFolder = "META-INF";
  private static String oebpsFolder = "";// "OEBPS";
  private static String containerFile = "container.xml";
  private static String contentPpfFile = "content.opf";
  private static String tocFile = "toc.ncx";
  private static String baseFileName;
  private static String baseFilePathHTML;
  private static String baseFileNameHTML;
  private String baseFilePath;
  private ZipOutputStream zip;
  private String baseFileZipPath;
  
  public ToEPub(String fileName) throws IOException {
      File file = new File(fileName);
      rootDir = file.getParent();
      baseFileName = file.getName();
      baseFilePath = file.getPath();
      baseFilePathHTML = baseFilePath + ".html";
      baseFileNameHTML = baseFileName.substring(0, baseFileName
              .lastIndexOf("."))
              + ".html";
      baseFileZipPath = baseFilePath.substring(0, baseFilePath
              .lastIndexOf("."))
              + ".epub";
      zip = new ZipOutputStream(new FileOutputStream(baseFileZipPath));
  
  }
  
  public void start() throws IOException {
  
      createMimeFile();
      createMeta();
      readConnent();
      createContentList();
      createTocFile();
  
      //zip.flush();
      zip.close();
      /*
       * Reading Creating Zip again
       */
      ZipFile zfile = new ZipFile(new File(baseFileZipPath));
      System.out.println(zfile.size() + " Files");
      Enumeration&lt;? extends ZipEntry&gt; itr = zfile.entries();
      while(itr.hasMoreElements()){
          ZipEntry entry = itr.nextElement();
          System.out.println(entry.getName()+" Size: "+entry.getCompressedSize()+"/"+entry.getSize() +" CRC: "+ entry.getCrc());
  
      }
  }
  
  public void createMimeFile() throws IOException {
      byte[] content = "application/epub+zip".getBytes("UTF-8");
      ZipEntry entry = new ZipEntry("mimetype");
      entry.setMethod(ZipEntry.STORED);
      entry.setSize(20);
      entry.setCompressedSize(20);
      entry.setCrc(0x2CAB616F); // pre-computed
      zip.putNextEntry(entry);
  
      zip.write(content);
      zip.closeEntry();
  
  }
  
  public void readConnent() throws IOException {
      String thisLine;
      FileReader fr = new FileReader(baseFilePath);
      BufferedReader bReader = new BufferedReader(fr);
      String outFile = oebpsFolder + File.separator + baseFileNameHTML;
      ZipEntry entry = new ZipEntry(baseFileNameHTML);
      zip.putNextEntry(entry);
      zip.write("&lt;?xml version='1.0' encoding='utf-8'?&gt;\r\n".getBytes("UTF-8"));
      zip.write("&lt;html xmlns=\"http://www.w3.org/1999/xhtml\"&gt;\r\n"
              .getBytes("UTF-8"));
      while ((thisLine = bReader.readLine()) != null) { // while loop
          // begins here
          zip.write(("&lt;p&gt;" + thisLine + "&lt;/p&gt;\r\n").getBytes("UTF-8"));
      } // end while
      zip.write("&lt;/html&gt;".getBytes("UTF-8"));
      zip.closeEntry();
  }
  
  public void createContentList() throws IOException{
      ZipEntry entry = new ZipEntry(contentPpfFile);
      zip.putNextEntry(entry);
       zip.write("&lt;?xml version='1.0' encoding='utf-8'?&gt;\r\n".getBytes("UTF-8"));
       zip.write("&lt;package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\" unique-identifier=\"uuid_id\"&gt;".getBytes("UTF-8"));
       zip.write("&lt;metadata xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:opf=\"http://www.idpf.org/2007/opf\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:calibre=\"http://calibre.kovidgoyal.net/2009/metadata\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"&gt;\r\n".getBytes("UTF-8")); 
       zip.write("&lt;dc:language&gt;en&lt;/dc:language&gt;\r\n".getBytes("UTF-8"));
       zip.write("&lt;dc:creator opf:role=\"aut\"&gt;Open Mobile Alliance&lt;/dc:creator&gt;\r\n".getBytes("UTF-8"));
  
       zip.write("&lt;meta name=\"calibre:timestamp\" content=\"2011-11-11T15:02:14.075083+00:00\"/&gt;\r\n".getBytes("UTF-8")); 
       zip.write("&lt;dc:title&gt;OMA Specification&lt;/dc:title&gt;\r\n".getBytes("UTF-8"));
       zip.write("&lt;dc:contributor opf:role=\"bkp\"&gt;calibre (0.8.22) [http://calibre-ebook.com]&lt;/dc:contributor&gt;\r\n".getBytes("UTF-8"));
       zip.write("&lt;dc:identifier id=\"uuid_id\" opf:scheme=\"uuid\"&gt;22fb9171-78bd-4bd1-89f8-7cae3a497c7c&lt;/dc:identifier&gt;\r\n".getBytes("UTF-8")); 
       zip.write("&lt;dc:subject&gt;Template&lt;/dc:subject&gt;\r\n".getBytes("UTF-8"));
       zip.write("&lt;/metadata&gt;\r\n".getBytes("UTF-8")); 
       zip.write("&lt;manifest&gt;\r\n".getBytes("UTF-8"));
       zip.write(("&lt;item href=\""+baseFileNameHTML+"\" id=\"id1\" media-type=\"application/xhtml+xml\"/&gt;\r\n").getBytes("UTF-8"));
       zip.write("&lt;item href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\" id=\"ncx\"/&gt;\r\n".getBytes("UTF-8")); 
       zip.write("&lt;/manifest&gt;\r\n".getBytes("UTF-8")); 
       zip.write("&lt;spine toc=\"ncx\"&gt;\r\n".getBytes("UTF-8"));
       zip.write("&lt;itemref idref=\"id1\"/&gt;\r\n".getBytes("UTF-8")); 
       zip.write("&lt;/spine&gt;\r\n".getBytes("UTF-8"));
       zip.write("&lt;guide/&gt;\r\n".getBytes("UTF-8")); 
       zip.write("&lt;/package&gt;\r\n".getBytes("UTF-8")); 
       zip.closeEntry();
  }
  public void createTocFile() throws IOException{
      ZipEntry entry = new ZipEntry(tocFile);
      zip.putNextEntry(entry);
      zip.write("&lt;?xml version=\"1.0\"?&gt;\r\n".getBytes("UTF-8"));
      zip.write("&lt;container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\"&gt;\r\n".getBytes("UTF-8")); 
      zip.write("&lt;rootfiles&gt;".getBytes("UTF-8"));
  
      zip.write(("&lt;rootfile full-path=\""+contentPpfFile
                +"\" media-type=\"application/oebps-package+xml\"/&gt;\r\n").getBytes("UTF-8"));
  
      zip.write("&lt;/rootfiles&gt;\r\n".getBytes("UTF-8")); 
      zip.write("&lt;/container&gt;".getBytes("UTF-8"));
      zip.closeEntry();
  }
  public void createMeta() throws IOException{
  
      ZipEntry entry = new ZipEntry(metaFolder+File.separator+containerFile);
  
      zip.putNextEntry(entry);
      zip.write("&lt;?xml version=\"1.0\"?&gt;\r\n".getBytes("UTF-8"));
      zip.write("&lt;container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\"&gt;\r\n".getBytes("UTF-8"));
      zip.write("&lt;rootfiles&gt;\r\n".getBytes("UTF-8"));
      zip.write("&lt;rootfile full-path=\"content.opf\" media-type=\"application/oebps-package+xml\"/&gt;\r\n".getBytes("UTF-8"));
      zip.write("&lt;/rootfiles&gt;\r\n".getBytes("UTF-8"));
      zip.write("&lt;/container&gt;\r\n".getBytes("UTF-8"));
      zip.closeEntry();
  }
  public void createMetaInfo() throws IOException{
      ZipEntry entry = new ZipEntry(metaFolder+File.separator);
      zip.putNextEntry(entry);
      zip.closeEntry();
  }
  
  }
  </code></pre>
  
  <p>I tried change UTF-8 to default but not avail</p>
  
  <p>Thanks in advance</p>