【问题标题】:how to decode/ get encoding of file (Power BI desktop file)如何解码/获取文件编码(Power BI 桌面文件)
【发布时间】:2018-08-09 10:29:35
【问题描述】:

我正在尝试解码 Power BI 桌面报表 (pbix) 内部文件 (DataMashup)。 我的目标是使用任何编程语言创建 Power-BI 桌面报表、数据模型。我使用 Java 作为初始值。

文件使用某种编码技术进行编码。

我尝试获取文件的编码,它返回 windows 1254。但没有发生解码。

File f = new File("example.txt");

    String[] charsetsToBeTested = {"UTF-8", "windows-1254", "ISO-8859-7"};

    CharsetDetector cd = new CharsetDetector();
    Charset charset = cd.detectCharset(f, charsetsToBeTested);

    if (charset != null) {
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(f), charset);
            int c = 0;
            while ((c = reader.read()) != -1) {
                System.out.print((char)c);
            }
            reader.close();
        } catch (FileNotFoundException fnfe) {
            fnfe.printStackTrace();
        }catch(IOException ioe){
            ioe.printStackTrace();
        }

    }else{
        System.out.println("Unrecognized charset.");
    }

文件解压也不行

public void unZipIt(String zipFile, String outputFolder)
{
    byte buffer[] = new byte[1024];
    try
    {
        File folder = new File(outputFolder);
        if(!folder.exists())
        {
            folder.mkdir();
        }
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        System.out.println(zis);

        System.out.println(zis.getNextEntry());
        for(ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry())
        {
            String fileName = ze.getName();
            System.out.println(ze);
            File newFile = new File((new StringBuilder(String.valueOf(outputFolder))).append(File.separator).append(fileName).toString());
            System.out.println((new StringBuilder("file unzip : ")).append(newFile.getAbsoluteFile()).toString());
            (new File(newFile.getParent())).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while((len = zis.read(buffer)) > 0) 
            {
                fos.write(buffer, 0, len);
            }
            fos.close();
        }

        zis.closeEntry();
        zis.close();
        System.out.println("Done");
    }
    catch(IOException ex)
    {
        ex.printStackTrace();
    }
}

【问题讨论】:

    标签: java powerbi powerbi-datasource


    【解决方案1】:

    该文件包含一个二进制标头,然后是指定了 UTF-8 的 XML。 标头数据似乎包含文件名(Config/Package.xml),因此假设 zip 格式是可以理解的。对于 zip 格式,文件末尾也会有二进制数据。

    可能文件是使用 FTP 下载的,并且完成了文本转换(“\n”到“\r\n”)。然后zip会损坏。将文件重命名为 .zip 可能有助于使用 zip 工具测试文件。

    首先尝试 .tar 格式。这是合乎逻辑的,因为 XML 文件没有被压缩。将 .tar 添加到文件结尾。

    否则,如果内容始终是 UTF-8 XML:

    Path f = Paths.get("example.txt");
    String start ="<?xml";
    String end = ">";
    byte[] bytes = Files.readAllBytes(f);
    String s = new String(bytes, StandardCharsets.ISO_8859_1); // Single byte encoding.
    int startI = s.indexOf(start);
    int endI = s.lastIndexOf(end) + end.length();
    //bytes = Arrays.copyOfRange(bytes, startI, endI);
    String xml = new String(bytes, startI, endI - startI, StandardCharsets.UTF_8);
    

    【讨论】:

    • 这个文件没有扩展名,我可以用7z软件解压,但是不能用winrar。更改文件后我无法再次压缩它,解压缩此文件后,我得到 Config 文件夹、Formula 文件夹和一个 xml 文件。所有子文件内容不同。
    • 如果我对 DataMashup 文件的 xml 进行任何更改并创建打包的 power bi 报告文件 (pbix),它会抛出错误,指出文件已损坏。
    【解决方案2】:

    您可以使用 System.IO.Packaging 库来提取 Power BI 数据混搭。它使用 OPC 封装标准,请参阅here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多